C# RegEx Cheat Sheet
Basic Patterns
| Pattern | Description |
|---|---|
| . | Matches any single character except a newline. |
| ^ | Matches the start of a string. |
| $ | Matches the end of a string. |
| * | Matches 0 or more repetitions of the preceding character or group. |
| + | Matches 1 or more repetitions of the preceding character or group. |
| ? | Matches 0 or 1 repetition of the preceding character or group. |
| {n} | Matches exactly n repetitions of the preceding character or group. |
| {n,} | Matches n or more repetitions of the preceding character or group. |
| {n,m} | Matches between n and m repetitions of the preceding character or group. |
Character Classes
| Pattern | Description |
|---|---|
| \d | Matches any digit (0-9). |
| \D | Matches any non-digit. |
| \w | Matches any word character (alphanumeric + underscore). |
| \W | Matches any non-word character. |
| \s | Matches any whitespace character (spaces, tabs, line breaks). |
| \S | Matches any non-whitespace character. |
Assertions
| Pattern | Description |
|---|---|
| \b | Matches a word boundary. |
| \B | Matches a non-word boundary. |
| (?=...) | Positive lookahead. |
| (?!...) | Negative lookahead. |
| (?<=...) | Positive lookbehind. |
| (? | Negative lookbehind. |
Groups and Backreferences
| Pattern | Description |
|---|---|
| ( ... ) | Captures the matched content. |
| (?: ... ) | Non-capturing group. |
| \1 | Backreference to the first capturing group. |
| \2 | Backreference to the second capturing group, and so on. |
Character Escapes
| Pattern | Description |
|---|---|
| \a | Matches a bell character (alarm, \x07). |
| \e | Matches an escape character (\x1B). |
Unicode Categories
| Pattern | Description |
|---|---|
| \p{name} | Matches any character in the named category, e.g., \p{Lu} matches any uppercase letter. |
| \P{name} | Matches any character not in the named category. |
Miscellaneous
| Pattern | Description |
|---|---|
| (? |
Named capturing group. |
| \k |
Backreference to a named group. |
| (?(expression)yes|no) | Conditional matching. |
| (?#...) | Inline comment. |
| (?imnsx-imnsx) | Sets or clears specific options within a subexpression: i (case-insensitive), m (multi-line mode), n (explicit capture), s (single-line mode), x (ignore pattern whitespace). |
| (?imnsx-imnsx:...) | Group with specific options. |
Atomic Grouping and Possessive Quantifiers
| Pattern | Description |
|---|---|
| (?>...) | Atomic group. |
| *+ | Possessive quantifier (0 or more). |
| ++ | Possessive quantifier (1 or more). |
| ?+ | Possessive quantifier (0 or 1). |
Balancing Group Definitions
| Pattern | Description |
|---|---|
| (? |
Defines a balancing group definition. |
Zero-width Positive Lookbehind and Zero-width Negative Lookbehind
| Pattern | Description |
|---|---|
| (?<=...) | Zero-width positive lookbehind. |
| (? | Zero-width negative lookbehind. |
Options
| Pattern | Description |
|---|---|
| (?imnsx-imnsx) | Sets or clears specific options within a subexpression: i (case-insensitive), m (multi-line mode), n (explicit capture), s (single-line mode), x (ignore pattern whitespace). |
| (?imnsx-imnsx:...) | Group with specific options. |
Right-to-left Mode
| Pattern | Description |
|---|---|
| (?<) | Zero-width positive lookbehind assertion. |
| (?<!) | Zero-width negative lookbehind assertion. |
Named Blocks
| Pattern | Description |
|---|---|
| (?<name>...) | Named capturing group. |
| \k<name> | Backreference to a named group. |
Non-capturing Group
| Pattern | Description |
|---|---|
| (?:...) | Non-capturing group. |
Atomic Zero-width Assertions
| Pattern | Description |
|---|---|
| (?=...) | Zero-width positive lookahead assertion. |
| (?!...) | Zero-width negative lookahead assertion. |
Comments
| Pattern | Description |
|---|---|
| (?#...) | Inline comment. |
Options Setting
| Pattern | Description |
|---|---|
| (?imnsx) | Set the options for the remainder of the regex. |
| (?-imnsx) | Unset the options for the remainder of the regex. |
Conditional Matching
| Pattern | Description |
|---|---|
| (?(expression)yes|no) | Conditional matching. |