Regex Cheat Sheet - .NET

A comprehensive guide to regular expressions with patterns, examples, and explanations

Basic Metacharacters

.Any character except newline
\\Escape character (double backslash in C# strings)
|Alternation (OR)
^Start of string/line
$End of string/line

Character Classes

[abc]Any of a, b, or c
[^abc]Not a, b, or c
[a-z]Range from a to z
\\dDigit [0-9]
\\DNon-digit [^0-9]
\\wWord character [a-zA-Z0-9_]
\\WNon-word character
\\sWhitespace
\\SNon-whitespace

Quantifiers

*0 or more (greedy)
+1 or more (greedy)
?0 or 1 (greedy)
{n}Exactly n times
{n,}n or more times
{n,m}Between n and m times
*?0 or more (lazy)
+?1 or more (lazy)
??0 or 1 (lazy)

Groups and Capturing

(pattern)Capturing group
(?:pattern)Non-capturing group
(?<name>pattern)Named capturing group
(?'name'pattern)Named capturing group (alternative syntax)
\\1Backreference to group 1
\\k<name>Named backreference
\\k'name'Named backreference (alternative)
(?>pattern)Atomic group

Lookarounds

(?=pattern)Positive lookahead
(?!pattern)Negative lookahead
(?<=pattern)Positive lookbehind
(?<!pattern)Negative lookbehind

Conditional Constructs

(?(expression)yes|no)Conditional match
(?(name)yes|no)Conditional based on named group
(?(1)yes|no)Conditional based on numbered group

RegexOptions Flags

RegexOptions.IgnoreCaseCase insensitive matching
RegexOptions.Multiline^ and $ match line breaks
RegexOptions.SinglelineDot matches newline
RegexOptions.ExplicitCaptureOnly named groups capture
RegexOptions.CompiledCompile regex to IL
RegexOptions.IgnorePatternWhitespaceAllow comments and whitespace
RegexOptions.RightToLeftSearch from right to left
RegexOptions.ECMAScriptECMAScript-compliant behavior

Unicode

\\p{L}Unicode letter
\\p{N}Unicode number
\\p{P}Unicode punctuation
\\p{IsLatin}Latin script block
\\p{IsBasicLatin}Basic Latin Unicode block
\\P{L}Not Unicode letter