Visual Basic Regular Expressions

A regular expression, or regex for short, is a sequence of characters that form a search pattern, to find in another piece of text, for example, to see if a piece of text matches a certain format or contains particular characters. These can be used, for example, in ‘if’ statements, to verify data. The example below looks for the word ‘Basic’ in ‘Visual Basic’ and displays a message if it is found or not. In order for this, or any other regular expression to work, there must be an imports statement for ‘System.Text.RegularExpressions’.

If Regex.IsMatch("Visual Basic", "Basic") Then

   Console.WriteLine("Match found")

Else

   Console.WriteLine("No match found")

End If

In this example, the ‘IsMatch’ method of the ‘Regex’ class is used, to find the second string contained in the brackets that follow, within the first string that is specified. As the word ‘Basic’ exists in the string ‘Visual Basic’, ‘Match found’ is displayed in the console.

Regular Expression Characters

Regular expression characters are special characters that can be used in a search pattern, for example, to find specific characters at the beginning or end of a search pattern, within a particular range and much more. Below is a list of the regular expression characters available within Visual Basic, together with some examples. All of the examples would produce a successful match.

Character Description Example
. Matches a single character. Regex.IsMatch(“Basic”, “B…c”)
[value] Matches at least one character specified within the brackets. Regex.IsMatch(“Basic”, “B[aeiou]sic”)
[range] Matches at least one character specified within a range. Regex.IsMatch(“Basic”, “Bas[f-k]c”)
[^value] Matches any character except those within the brackets. Regex.IsMatch(“Basic”, “Ba[^aeiou]ic”)
^ Matches characters located at the beginning of a string. Regex.IsMatch(“Basic”, “^Bas”)
$ Matches characters located at the end of a string. Regex.IsMatch(“Basic”, “sic$”)
* Matches any instances of the preceding characters. Regex.IsMatch(“Basic”, “asi*”)
? Matches zero or one occurrence of the preceding characters. Regex.IsMatch(“Basic”, “asi?”)
\ Matches the character that follows as an escaped character. Regex.IsMatch(“Basic/C#”, “\/”)

Regular Expression Qualifiers

Regular expression qualifiers are used to help make the search pattern more specific, for example, a match must occur a specific number of times or within a specific range. Below is a list of qualifiers that are available within Visual Basic.

Qualifier Description
* Must match zero or more times.
+ Must match one or more times.
? Must match no more than one time.
{n} Must match n times.
{n,} Must match at least n times.
{n,m} Must match at least n time, but not more than m times.

Regular Expression Shortcuts

To make creating regular expressions easier, a number of shortcuts are available, which can be found below.

Shortcut Description
\d Matches any decimal digit. Equivalent to [0-9].
\w Matches any word character. Equivalent to [0-9A-Za-z_].
\D Matches any non-digit.
\W Matches any non-word character such as space.
\S Matches any non-whitespace character.

Example Regular Expressions

Below are examples of regular expressions that utilise some of the characters, qualifiers and shortcuts above.

Example Description
^[A-Z]{2}[0-9]{6}[A-Z]{1}$ Check the format of a valid UK National Insurance Number. Note, this only checks for a valid format of two letters, followed by six digits and then one more letter. It doesn’t enforce all the rules, for example, the first letter cannot be D, F, I, Q, U or Z.
^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$ Check for a valid IP address, one to three digits followed by a period, then another one to three digits and so on, e.g. “192.168.15.20”.
^\d{3}-\d{2}-\d{4}$ Check for a valid American Social Security Number, three digits, followed by a hyphen, two digits, followed by another hyphen and then four more digits.