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/lineCharacter Classes
[abc]
Any of a, b, or c[^abc]
Not a, b, or c[a-z]
Range from a to z\\d
Digit [0-9]\\D
Non-digit [^0-9]\\w
Word character [a-zA-Z0-9_]\\W
Non-word character\\s
Whitespace\\S
Non-whitespaceQuantifiers
*
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)\\1
Backreference to group 1\\k<name>
Named backreference\\k'name'
Named backreference (alternative)(?>pattern)
Atomic groupLookarounds
(?=pattern)
Positive lookahead(?!pattern)
Negative lookahead(?<=pattern)
Positive lookbehind(?<!pattern)
Negative lookbehindConditional Constructs
(?(expression)yes|no)
Conditional match(?(name)yes|no)
Conditional based on named group(?(1)yes|no)
Conditional based on numbered groupRegexOptions Flags
RegexOptions.IgnoreCase
Case insensitive matchingRegexOptions.Multiline
^ and $ match line breaksRegexOptions.Singleline
Dot matches newlineRegexOptions.ExplicitCapture
Only named groups captureRegexOptions.Compiled
Compile regex to ILRegexOptions.IgnorePatternWhitespace
Allow comments and whitespaceRegexOptions.RightToLeft
Search from right to leftRegexOptions.ECMAScript
ECMAScript-compliant behaviorUnicode
\\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