C# Regex Cheat Sheet

In C#, Regex

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

SyntaxDescription
.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

SyntaxDescription
\dMatches any digit character.
\DMatches any non-digit character.
\sMatches any whitespace character.
\SMatches any non-whitespace character.
\wMatches any word character.
\WMatches any non-word character.

Anchors

SyntaxDescription
\bMatches a word boundary.
\BMatches a non-word boundary.

Quantifiers

SyntaxDescription
*?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

SyntaxDescription
(?=...)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

SyntaxDescription
( )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

SyntaxDescription
$nInserts the nth captured group.
${name}Inserts the captured group with the specified name.
$&Inserts the entire matched string.
`$“Inserts the text preceding the match.
$'Inserts the text following the match.

References

# #