C# / .NET Regex Tester
Test regular expressions written for C# / .NET (System.Text.RegularExpressions) — with warnings for anything that works differently there, and C# / .NET code to copy.
//g
C# / .NET regex syntax at a glance
- Literal
- new Regex(@"pattern") — a verbatim string, where " is written ""
- Named group
- (?<name>…) or (?'name'…), back-reference \k<name>
- Replacement
- $1, ${name}, $0
- Flags
- RegexOptions.IgnoreCase, Multiline, Singleline (dot matches \n), IgnorePatternWhitespace — or inline (?i)
- Find all
- foreach (Match m in regex.Matches(text))
What's different in C# / .NET
- →\d matches any Unicode digit (Arabic-Indic, Devanagari…), not just 0–9 — use [0-9] or RegexOptions.ECMAScript when that matters.
- →Lookbehind can be any length, and balancing groups (?<open-close>…) can match nested brackets.
- →There are no possessive quantifiers; use an atomic group (?>…) instead.
- →Pass a match timeout (new Regex(pattern, options, TimeSpan.FromSeconds(1))) for patterns that run on user input, and prefer [GeneratedRegex] in .NET 7+.
Frequently asked
Why does \d match characters that aren't 0–9 in C#?
.NET follows Unicode: \d is any decimal digit, including other scripts. Use [0-9], or RegexOptions.ECMAScript to get ASCII-only behavior.
What does RegexOptions.Singleline do?
It makes . match newline characters too (other languages call this dotAll or DOTALL). It doesn't change ^ and $ — that's RegexOptions.Multiline.