Why a .NET-specific tester
Most online regex testers run JavaScript's RegExp or PCRE. .NET's engine is its own flavour, and the differences decide whether a pattern works in your C# code:
\dand\ware Unicode by default:\dmatches any Unicode decimal digit (Arabic-Indic٣, Devanagari३) and\wmatches letters from every script. Validating "digits only" with^\d+$accepts more than 0-9. Use[0-9]orRegexOptions.ECMAScript.- A group inside a quantifier, like
(\w)+, remembers every repetition inGroup.Captures, not just the last one. - Balancing groups,
(?<open>\()and(?<-open>\)), let .NET match nested parentheses, which other engines cannot. - Lookbehind of any length,
RightToLeft,\G,(?n)inline options and conditionals(?(name)yes|no)are also .NET-only. - IgnoreCase depends on the culture. Without
RegexOptions.CultureInvariant, case-insensitive matching uses the current culture for the Turkish dotted and dotless i: under en-US,(?i)ialso matchesİ; under tr-TR,ipairs withİandIwithı. This page uses invariant casing, which is whatCultureInvariantgives you on every machine. We checked this against .NET 10 on Linux; every other pattern we tried matched the same in all three cultures. Regex.Splitincludes captured groups:Regex.Split("a1b", "(\d)")givesa,1,b.
[GeneratedRegex] or new Regex?
Since .NET 7 the regex source generator turns a [GeneratedRegex] partial method into plain C# at build time. There is no parsing or compiling at run time, it works with trimming and Native AOT, and an invalid pattern is a build error instead of an exception in production. For patterns known at compile time, prefer it. If you build patterns at run time, create each Regex once and reuse it.
The same pattern in PowerShell
PowerShell runs on .NET, so -match, -replace, -split and [regex] use the same engine and give the same results as this tester. Two things differ from C#:
- The operators ignore case by default:
'ABC' -match 'abc'isTrue. Use-cmatch,-creplaceand-csplitfor case-sensitive matching, or[regex]with explicit options, as the PowerShell snippet here does. - Put replacement patterns in single quotes. In
"$1"PowerShell expands$1as its own variable (usually empty) before the regex sees it;'[$1]'reaches the regex intact.
The PowerShell code is tested with PowerShell 7.6 on .NET 10. Windows PowerShell 5.1 runs on .NET Framework, which has no NonBacktracking option.
Timeouts and catastrophic backtracking
Patterns with nested quantifiers, such as (a+)+$ or (\w+\s?)*$, can take exponential time on inputs that almost match, which is a denial-of-service risk with user input. Always pass a timeout (it throws RegexMatchTimeoutException), or use RegexOptions.NonBacktracking (.NET 7+), which runs in linear time but rejects lookarounds, backreferences, atomic groups and RightToLeft. Try the catastrophic backtracking example above.
FAQ
Why does my regex work in JavaScript but not in C#?
The engines differ in Unicode classes, lookbehind, captures and options. The tester shows below the results when JavaScript's RegExp would match differently.
Why does Regex.Split return extra items?
Captured groups in the pattern are included in the result. Use (?:...) or RegexOptions.ExplicitCapture.
What does [GeneratedRegex] do?
It generates the regex code at compile time (.NET 7+): faster start-up, AOT-friendly, and invalid patterns fail the build.
Why does -match ignore case in PowerShell?
PowerShell's comparison operators are case-insensitive by default. Use -cmatch for a case-sensitive match.
How do I stop a regex from hanging?
Use a match timeout, or RegexOptions.NonBacktracking when you do not need lookarounds or backreferences.