Understand C# regular expression symbols

The following table shows useful regular expression symbols, commands, and other items that you can use in regular expressions in C# programs.

Item Purpose
\ Either begins a special symbol such as \n or escapes the following character
^ Matches the beginning of string (or line if in multiline mode)
$ Matches the end of string (or line if in multiline mode)
\A Matches the beginning of string (even if in multiline mode)
\Z Matches the end of string (even if in multiline mode)
* Matches the preceding character zero or more times
+ Matches the preceding character one or more times
? Matches the preceding character zero or one time
. Matches any character
[abc] Matches any one of the characters inside the brackets
[^abc] Matches one character that is not inside the brackets
[a-z] Matches one character in the range of characters
[^a-z] Matches one character that is not in the range of characters
x|y Matches x or y
(pattern) Makes a match group
(?<name>pattern) Makes a match group and gives it a name
\n Refers to a previously defined group
(?:pattern) Make a non-capturing group (a group that you can’t later refer to)
{n} Matches exactly n occurrences
{n,} Matches n or more occurrences
{n,m} Matches between n and m occurrences
(?=…) Positive lookahead (must match the pattern at this point)
(?!…) Negative lookahead (must not match the pattern at this point)
(?<=…) Positive lookbehind (the preceding text must match the pattern)
(?<!–…) Negative lookbehind (the preceding text must not match the pattern)
\b Matches a word boundary
\B Matches a non-word boundary
\d Matches a digit
\D Matches a non-digit
\f Matches a form-feed
\n Matches a newline
\r Matches a carriage return
\s Matches white space (space, tab, form-feed, etc.)
\S Matches non-white space
\t Matches a tab
\v Matches a vertical tab
\w Matches a word character (includes underscore)
\W Matches a non-word character

Note that the regular expressions used by .NET are not exactly the same as those used by some other programming languages. If you’re searching the internet for regular expression information, be sure you’re looking at a .NET friendly version or you could get pretty confused.


Follow me on Twitter   RSS feed   Donate




This entry was posted in regular expressions, strings and tagged , , , , , , , , , , , . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *