March 6th, 2009
C# Regular Expression Cheat Sheet - 2
If you're new here, you may want to subscribe to my RSS feed. Thanks for visiting!
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. This post contains the C# regular expression operators as used by the .NET regular expression classes such as RegEx.
If you would like to print this, click here for a pure HTML version.
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 |
| \040 | 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 "input">{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
"parameter">n times. It is a lazy quantifier that is the counter to the greedy quantifier "input">{n}+. |
Tags: regex









Except where otherwise noted, content on this site is
October 28th, 2009 at 7:52 pm
My brain hurts.
March 10th, 2011 at 5:57 am
[...] C# Regular Expression Cheat Sheet Posted on March 9, 2011 by selcukterzi Quote:http://www.dijksterhuis.org/csharp-regular-expression-operator-cheat-sheet/#more-769 [...]