Home About

March 6th, 2009

C# Regular Expression Cheat Sheet - 2

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}+.
Be Sociable, Share!

Tags:

2 Responses to “C# Regular Expression Cheat Sheet”

  1. Danyal Says:

    My brain hurts.

  2. C# Regular Expression Cheat Sheet | selcukterzi Says:

    [...] 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 [...]


Most popular

    Sorry. No data so far.

Recent Comments
  • Juan Romero: Hi there, it’s a neat little class, but I believe you could do the same thing with the WebClient...
  • anthosh: Hey, THank you very much for your tutorial. It was awesome. But i have a problem that i am not able to...
  • bian: how to get passphase if i have encrypt and decrypt string?? Thanks alot
  • Michael: Hi, I really like your post, thanks a lot, it really helped clear up a few things I could not remember how...
  • Bharat Prajapati: i was trying to import keyword dictionary to this plugin which is in csv format, but i get an error...