C# 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 ‘C#’ in ‘C# Programming’ and displays a message if it is found or not. In order for this, or any other regular expression to work, there must be a ‘using’ directive for ‘System.Text.RegularExpressions’.

if (Regex.IsMatch(@"C# Programming", @"C#"))
{

    Console.WriteLine("Match found");

}
else
{

    Console.WriteLine("No match found");

}

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 ‘C#’ exists in the string ‘C# Programming’, ‘Match found’ is displayed in the console. The ‘@’ symbol, before both quoted strings within the brackets, allows for a string to be specified without having to escape any characters. If, for example, a ‘\’ existed in either of the strings, it would have to be represented as ‘\\’, because it has a special purpose in C#. Including the ‘@’ symbol means that you don’t have to do this. In the example above it isn’t necessary to include the ‘@’ because there are no characters in either string to cause a problem, so it is personal choice whether to include it or not.

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 C#, together with some examples. All of the examples would produce a successful match.

Character Description Example
. Matches a single character. Regex.IsMatch(@”Sharp”, @”S…p”)
[value] Matches at least one character specified within the brackets. Regex.IsMatch(@”Sharp”, @”Sh[aeiou]rp”)
[range] Matches at least one character specified within a range. Regex.IsMatch(@”Sharp”, @”Sha[p-t]p”)
[^value] Matches any character except those within the brackets. Regex.IsMatch(@”Sharp”, @”S[^aeiou]arp”)
^ Matches characters located at the beginning of a string. Regex.IsMatch(@”Sharp”, @”^Sha”)
$ Matches characters located at the end of a string. Regex.IsMatch(@”Sharp”, @”arp$”)
* Matches any instances of the preceding characters. Regex.IsMatch(@”Sharp”, @”har*”)
? Matches zero or one occurrence of the preceding characters. Regex.IsMatch(@”Sharp”, @”har?”)

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 C#.

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
Regex.IsMatch(@”AA123456A”, @”^[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.
Regex.IsMatch(@”192.168.15.20″, @”^\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”.
Regex.IsMatch(@”192-68-1520″, @”^\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.