C# Regular Expression

Regular expression help in splitting, matching, searching and replacing text based on a test pattern. It provide function for verifying that string matches a pattern. For example ^\d{5}-\d{5}$ pattern matches 5 digits followed by a hyphen followed by 5 more digits as in 88765-85465.

System.Text.RegularExpressions namespace provide regular expression classes. You may want to include System.Text.RegularExpressions namespace with a using directive.

How to build a regular expressions pattern?

A string must match exactly characters that have special meanings. For example, [0-9] means string must match a single digit 0 to 9. Regular expression have special character sequences called escape sequences. For example \d means match a single digit 0 to 9.

If you want to ignore special character in pattern than we used '\'. For example \ or [ escape it as in \\ or \[.

The regular express divided into following categories:

  • Character escape
  • Character class
  • Anchor
  • Grouping construct
  • Quantifier
  • Alternation construct

Character escape

A character escape are those special character such as [Tab].

Escape Meaning
\t Match the tab character
\r Match the return character
\n Match the newline character

Character class

A character class are those set of characters matches with string.

Constract Meaning
[chars] Match one of the characters
[^chars] Match a character that is not include
[a–z] Match a character between the character a to z.


An anchor represents a state that string match in certain point to achieve. For example ^ and $ character represent the beginning and ending of a string.

Grouping construct

Grouping construct are that matching pieces of string. For Example 123-45-6789 you could define groups of the pieces 123, 45, and 6789.


A quantifier match the previous element a certain number of times. For example \d{4} means matches 4 digit in a string.

Quantifier Meaning
* Matches the previous element 0 or more times
+ Matches the previous element 1 or more times
? Matches the previous element 0 or 1 times
{n} Matches the previous element exactly n times

Alternation construct

An alternation construct you match two sub-expression either you used first or second sub-expression. For example ^(Yes | True)$ means matches either Yes or True.