C# Regular Expression Operator Cheat Sheet

I have been doing quite a bit with regular expressions recently and to avoid having to look them up again and again I made myself a little table with the most important C# regular expression operators and stuck it on the wall. Martijn’s C# Programming Blog

Escape Characters

Character Description
ordinary characters Characters other than . $ ^ { [ ( | ) * + ? \ match themselves.
. (dot) Matches any character
\w Matches any word character.
\W The negation of \w
\s Matches any white-space character.
\S Matches any non-white-space character.
\d Matches any decimal digit.
\D Matches any non-decimal digit.
\a Matches a bell (alarm) \u0007.
\b Matches a backspace \u0008 if in a [] character class
\t Matches a tab
\r Carriage return
\v Vertical tab
\f Form feed
\n New line
\e Matches an escape
40 Matches an ASCII character as octal (up to three digits);
\x20 Matches an ASCII character using hexadecimal representation (exactly two digits).
\cC Matches an ASCII control character; for example, \cC is control-C.
\u0020 Matches a Unicode character using hexadecimal representation (exactly four digits).
\ When followed by a character that is not recognized as an escaped character, matches that character. For example, \* is the same as \x2A.

 

Alternation

Alternation Definition
| Matches any one of the terms separated by the | (vertical bar) character; for example, cat|dog|tiger. The leftmost successful match wins.
(?(expression)yes|no) Matches the “yes” part if the expression matches at this point; otherwise, matches the “no” part.
(?(name)yes|no) Matches the “yes” part if the named capture string has a match; otherwise, matches the “no” part.

 

Substitutions

Character Description
$number Substitutes the last substring matched by group number number (decimal).
${name} Substitutes the last substring matched by a (?<name> ) group.
$$ Substitutes a single “$” literal.
$& Substitutes a copy of the entire match itself.
$` Substitutes all the text of the input string before the match.
$’ Substitutes all the text of the input string after the match.
$+ Substitutes the last group captured.
$_ Substitutes the entire input string.

 

Word boundaries

Assertion Description
^ Specifies that the match must occur at the beginning of the string or the beginning of the line.
$ Specifies that the match must occur at the end of the string, before \n at the end of the string, or at the end of the line.
\A Specifies that the match must occur at the beginning of the string (ignores the Multiline option).
\Z Specifies that the match must occur at the end of the string or before \n at the end of the string (ignores the Multiline option).
\z Specifies that the match must occur at the end of the string (ignores the Multiline option).
\G Specifies that the match must occur at the point where the previous match ended. When used with Match.NextMatch(), this ensures that matches are all contiguous.
\b Specifies that the match must occur on a boundary between \w (alphanumeric) and \W (nonalphanumeric) characters. The match must occur on word boundaries (that is, at the first or last characters in words separated by any nonalphanumeric characters). The match can also occur on a word boundary at the end of the string.
\B Specifies that the match must not occur on a \b boundary.

 

Quantifiers

* Matches the preceding element zero or more times. It is equivalent to {0,}. * is a greedy quantifier whose non-greedy equivalent is *?.
+ Matches the preceding element one or more times. It is equivalent to {1,}. + is a greedy quantifier whose non-greedy equivalent is +?.
? Matches the preceding element zero or one time. It is equivalent to {0,1}. ? is a greedy quantifier whose non-greedy equivalent is ??.
{n} Matches the preceding element exactly n times. {n} is a greedy quantifier whose non-greedy equivalent is {n}?.
{n,} Matches the preceding element at least n times. {n,} is a greedy quantifier whose non-greedy equivalent is {n}?.
{n,m} Matches the preceding element at least n, but no more than m, times. {n,m} is a greedy quantifier whose non-greedy equivalent is {n,m}?.
*? Matches the preceding element zero or more times, but as few times as possible. It is a lazy quantifier that is the counterpart to the greedy quantifier *.
+? Matches the preceding element one or more times, but as few times as possible. It is a lazy quantifier that is the counterpart to the greedy quantifier +.
?? Matches the preceding element zero or one time, but as few times as possible. It is a lazy quantifier that is the counterpart to the greedy quantifier ?.
{n}? Matches the preceding element exactly n times. It is a lazy quantifier that is the counter to the greedy quantifier {n}+.

About eagle081183

Passionate, Loyal
This entry was posted in ASP.NET MVC, C#. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s