C# is a powerful programming language that is widely used for developing applications and software. One of the most important features of C# is its support for regular expressions, or regex. Regular expressions are a powerful tool for searching and manipulating text, and they can be used in a wide range of applications, from data validation to text processing.
If you’re new to C# or regex, it can be difficult to remember all the syntax and rules for creating regular expressions. That’s where a C# regex cheat sheet comes in handy. A cheat sheet is a quick reference guide that provides a summary of the most important regex syntax and rules, making it easy to create regular expressions on the fly.
This cheat sheet provides a list of regular expressions in C# with their descriptions.
Basic Syntax
Syntax
Description
.
Matches any character except newline.
^
Matches the beginning of the string.
$
Matches the end of the string.
[]
Matches any character inside the brackets.
[^]
Matches any character not inside the brackets.
|
Matches either the expression before or after the operator.
()
Groups expressions together.
?
Matches zero or one occurrence of the preceding expression.
*
Matches zero or more occurrences of the preceding expression.
+
Matches one or more occurrences of the preceding expression.
{n}
Matches exactly n occurrences of the preceding expression.
{n,}
Matches n or more occurrences of the preceding expression.
{n,m}
Matches between n and m occurrences of the preceding expression.
\
Escapes special characters.
Character Classes
Syntax
Description
\d
Matches any digit character.
\D
Matches any non-digit character.
\s
Matches any whitespace character.
\S
Matches any non-whitespace character.
\w
Matches any word character.
\W
Matches any non-word character.
Anchors
Syntax
Description
\b
Matches a word boundary.
\B
Matches a non-word boundary.
Quantifiers
Syntax
Description
*?
Matches zero or more occurrences of the preceding expression, but as few as possible.
+?
Matches one or more occurrences of the preceding expression, but as few as possible.
??
Matches zero or one occurrence of the preceding expression, but as few as possible.
{n}?
Matches exactly n occurrences of the preceding expression, but as few as possible.
{n,}?
Matches n or more occurrences of the preceding expression, but as few as possible.
{n,m}?
Matches between n and m occurrences of the preceding expression, but as few as possible.
Lookarounds
Syntax
Description
(?=...)
Positive lookahead. Matches the preceding expression only if it is followed by the specified expression.
(?!...)
Negative lookahead. Matches the preceding expression only if it is not followed by the specified expression.
(?<=...)
Positive lookbehind. Matches the preceding expression only if it is preceded by the specified expression.
(?<!...)
Negative lookbehind. Matches the preceding expression only if it is not preceded by the specified expression.
Grouping and Capturing
Syntax
Description
( )
Groups expressions together.
(?: )
Groups expressions together, but does not capture the group.
(?<name> )
Captures the matched expression into a named group.
\k<name>
Matches the same text as previously matched by the named group.
Substitutions
Syntax
Description
$n
Inserts the nth captured group.
${name}
Inserts the captured group with the specified name.