Show Menu
Cheatography

.Net Regular Expressions Cheat Sheet (DRAFT) by

This is a draft cheat sheet. It is a work in progress and is not finished yet.

Characters

The following expres­sions will match single charac­ters. For more inform­ation see Micros­oft's article on Character Classes.

Characters

Ordinary characters
Characters other than . $ ^ { [ ( | ) * + ? \ match themse­lves.
.
Matches any character excluding the line feed. Includes the line feed in single­-line mode.
[abc]
A character class (may contain more than one charac­ter). Matches any character that is contained within the brackets, in no particular order.
[^abc]
The opposite of [ ]. Matches all characters not contained within the brackets.
[a-z]
Character range: Matches any single character in the range from first (a) to last (z).
\w
Matches an alpha-­numeric character (a-z, A-Z, 0-9, and unders­core).
\W
The opposite of \w. Matches any non-al­pha­numeric character.
\d
Matches a decimal character (0-9).
\D
The opposite of \d. Matches any non-de­cimal character.
\s
Matches a character of whitespace (space, tab, carriage return, line feed).
\S
The opposite of \s. Matches any non-wh­ite­space character.
\r
Matches a carriage return.
\n
Matches a new line (line feed).
\f
Matches a form feed.
\t
Matches a tab.
\v
Matches a vertical tab.
\a
Matches a bell character.
\b
In a character class, matches a backspace.
\e
Matches an escape.
\040
Uses octal repres­ent­ation to specify a character (octal consists of up to three digits).
\x20
Uses hexade­cimal repres­ent­ation to specify a character (hex consists of exactly two digits).
\c0003
Matches the specified 4-digit ASCII control character.
\u0020
Matches a Unicode character by using hexade­cimal repres­ent­ation (exactly four digits).
\p{name}
Matches any single character in the Unicode general category or named block specified by name.
\P{name}
Matches any single character that is not in the Unicode general category or named block specified by name.
\
In front of any of the special characters (. $ ^ { [ ( | ) * + ? \), this will match the character itself.
 

Assertions

The following expres­sions specify the location to search for a match, but do not match anything themse­lves.

Assertions

^
The match must start at the beginning of the string (or beginning of the line in multiline mode).
$
The match must occur at the end of the string or before \n at the end of the string (or end of the line in multiline mode).
\A
The match must occur at the start of the string.
\Z
The match must occur at the end of the string or before \n at the end of the string.
\z
The match must occur at the end of the string.
\G
The match must occur at the point where the previous match ended.
\b
Asserts a boundary between word and non-word charac­ters.
\B
The opposite of \b. Asserts a location that is not a boundary between word and non-word charac­ters.
(?=pat­tern)
Asserts that the specified pattern exists immedi­ately after this location. Known as a positive lookahead.
(?!pat­tern)
Asserts that the specified pattern does not exist immedi­ately after this location. Known as a negative lookahead.
(?<­=pa­ttern)
Asserts that the specified pattern exists immedi­ately before this location. Known as a positive lookbe­hind.
(?<­!pa­ttern)
Asserts that the specified pattern does not exist immedi­ately before this location. Known as a negative lookbe­hind.