C# Reference Β· Strings and Text
Regex syntax in C#
Cheat sheet for .NET regular expression syntax: character classes, anchors, quantifiers, groups, named captures, backreferences, lookarounds and escapes.
This page explains the pattern language understood by .NET's Regex. A pattern is made of literal characters, which match themselves, and metacharacters, which have special meanings: \ ^ $ . | ? * + ( ) [ ] { }. Put a backslash before a metacharacter to match it literally.
Patterns are easiest to write as verbatim (@"\d+") or raw string literals, because the backslash then reaches the regex engine untouched. The tables below group the constructs you will use most.
var date = new Regex(@"^(?<year>\d{4})-(?<month>\d{2})-(?<day>\d{2})$");
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input Β· what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with Console.ReadLine()), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input Β· what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with Console.ReadLine()), it gets the next line from this box.
Syntax
Character classes
| Pattern | Matches |
|---|---|
. |
Any character except \n (any character with Singleline) |
\d \D |
A digit (any Unicode decimal digit) / a non-digit |
\w \W |
A word character (letter, digit, underscore, and more) / its opposite |
\s \S |
Whitespace / non-whitespace |
[abc] [a-z] [^abc] |
One of the listed characters, a range, or anything but those |
\p{L} \p{Lu} \P{L} |
Unicode category (letter, uppercase letter) / its negation |
Anchors and boundaries
| Pattern | Matches |
|---|---|
^ $ |
Start / end of the input (of each line with Multiline); $ also matches before a final \n |
\A \z \Z |
Absolute start / absolute end / end or before a final \n |
\b \B |
Word boundary / not a word boundary |
Quantifiers
| Pattern | Meaning |
|---|---|
* + ? |
0 or more, 1 or more, 0 or 1 |
{n,} {n,m} |
Exactly n, at least n, between n and m |
*? +? ?? {n,m}? |
Lazy versions: as few as possible |
*+ and friends |
Not supported as possessive; use an atomic group (?>...) |
Groups and references
| Pattern | Meaning |
|---|---|
(x) |
Capturing group, numbered from 1 |
(?<name>x) |
Named capturing group |
(?:x) |
Non-capturing group |
(?>x) |
Atomic group (no backtracking into it) |
\1 \k<name> |
Backreference to a group |
x|y |
Alternation |
(?=x) (?!x) |
Positive / negative lookahead |
(?<=x) (?<!x) |
Positive / negative lookbehind |
(?i) (?s) (?m) (?x) |
Inline options for the rest of the group |
Errors / undefined behavior: an invalid pattern, such as an unbalanced parenthesis, a quantifier with nothing before it, or a reference to a group that does not exist, throws RegexParseException when the Regex is built.
Usage notes
- Greedy by default.
<.+>on<a><b>matches the whole string;<.+?>matches<a>. Prefer a precise class like<[^>]+>over lazy quantifiers. - Alternation order matters. The engine tries alternatives left to right and takes the first that leads to a match, not the longest. Put longer options first.
\dand\ware Unicode-aware.\dmatches digits from other scripts too. For plain ASCII use[0-9]orRegexOptions.ECMAScript.$is subtle. It matches at the very end and also just before a trailing\n. Use\zwhen you need exactly the end.- Lookbehind can be variable length in .NET, unlike many other engines, but keep it simple.
- Nested quantifiers are dangerous.
(a+)+or(\w+\s?)*can backtrack exponentially on near-misses. Use a timeout orNonBacktracking. - Comments. With
IgnorePatternWhitespace, you can lay a pattern out over several lines and use#comments. - Use a tester you trust, but always check behavior with .NET itself, since flavors (PCRE, JavaScript, Python) differ in details.