/ developer & network toolbox
← all tools

$ re.cs

runs locally

.NET / C# Regex Tester

Test .NET / C# style regex patterns against sample text online. The matching engine is JavaScript's, so .NET-specific differences are called out on the page.

regex-dotnet — invoker.tools

2 matches

foo@bar.com and info@example.com
@0foo@bar.com
@16info@example.com

JavaScript RegExp engine, runs in your browser.

About the .NET / C# Regex Tester

This tester is aimed at .NET and C# developers working with `System.Text.RegularExpressions.Regex`. You enter a pattern and sample text, set flags, and get live match highlighting, a match count and capture groups, the same feedback loop as calling Regex.Matches in a C# scratch project, minus the compile step.

To be direct about it: the engine running underneath is JavaScript's native RegExp, the same one every tester on this site uses, not the actual .NET regex engine. The two overlap on almost everything a typical pattern needs: character classes, quantifiers, groups, alternation, anchors, even named groups use compatible angle-bracket syntax in both. But .NET's regex engine has genuine extra capabilities, balancing groups, a built-in match timeout, right-to-left matching, that JavaScript simply does not have, and this page exists to name those gaps explicitly rather than let a pattern silently behave differently once it hits actual .NET code.

Use this to shape and debug the matching logic of a pattern quickly, then check the differences section below before relying on anything .NET-specific, particularly if the pattern involves balancing groups, RegexOptions beyond the basics, or a timeout.

Everything runs client-side. The pattern and test text stay in your browser and are never sent to a server.

How to use it

  1. Enter your pattern in the pattern field using standard regex syntax: character classes, quantifiers, groups, alternation and anchors.
  2. Named groups written as (?<name>...) work as-is here, since .NET and JavaScript happen to share that syntax; (?'name'...),NET's alternative quote-style syntax, does not translate directly and should be rewritten as (?<name>...) for testing.
  3. Set flags: i for case-insensitive (RegexOptions.IgnoreCase), m for multiline (RegexOptions.Multiline), s for dot-matches-newline (RegexOptions.Singleline, note the naming is reversed from what you might expect).
  4. Paste the sample text you want to test against, the same input you would pass to Regex.Match or Regex.Matches in C#.
  5. Review the live match count, highlighted matches and named or numbered capture groups.
  6. If the pattern relies on balancing groups, RightToLeft matching, or IgnorePatternWhitespace, note that those cannot be tested directly here; verify them in an actual .NET environment (LINQPad, a unit test, or the Regex class directly).
  7. Once confirmed, carry the pattern into your C# source as a verbatim string (prefixed with @) so backslashes do not need double-escaping.

Examples

  • Pattern (?<year>\d{4})-(?<month>\d{2})-(?<day>\d{2}) works identically here and in .NET, since both use the same (?<name>...) named-group syntax, one of the few places the two flavors line up exactly.
  • A .NET pattern written as (?'year'\d{4}) using single-quote group delimiters needs to be rewritten as (?<year>\d{4}) to test here, since this engine only recognizes the angle-bracket form.
  • @"\d+\.\d+" in C# source (a verbatim string containing \d+\.\d+) is entered here simply as \d+\.\d+; the @ prefix is a C# string-literal detail, not part of the pattern.
  • A balancing group like (?<open-close>\() used in .NET to match nested, balanced parentheses has no equivalent that can be typed into this tester, since JavaScript's RegExp has no balancing-group construct at all.
  • Regex.Matches(text, pattern, RegexOptions.IgnoreCase | RegexOptions.Multiline) in C# corresponds to entering the pattern here with flags im set, and running it with the g flag added so every match is returned rather than only the first.

.NET vs JavaScript regex syntax differences

.NET's regex engine and JavaScript's RegExp overlap more than most flavor pairs, both support named groups with the same angle-bracket delimiters, for instance, but .NET has several features with no JavaScript counterpart at all, and a couple of flags whose names mean something different than you would guess from JavaScript conventions.

  • Named groups: .NET accepts both (?<name>...) and (?'name'...); JavaScript only understands the angle-bracket form, so quote-style groups must be rewritten before testing here
  • Balancing groups: .NET-only construct (?<open-close>...) used to match nested or balanced constructs like parentheses or tags; JavaScript's RegExp has no equivalent and cannot express this at all
  • Verbose mode: RegexOptions.IgnorePatternWhitespace lets a .NET pattern span multiple lines with comments; JavaScript has no matching flag
  • Match timeout: the .NET Regex constructor accepts a matchTimeout parameter that aborts a runaway match; browser JavaScript has no built-in timeout, so a pathological pattern can hang the tab instead of throwing a clean exception
  • Right-to-left matching: RegexOptions.RightToLeft scans a string from the end backward; JavaScript has no equivalent option
  • Singleline naming: .NET's RegexOptions.Singleline actually makes . Match newlines (JavaScript calls the same behavior dotAll, flag s); .NET's Multiline changes ^ and $ the same way JavaScript's m flag does, so only the dot-matching flag's name is the confusing one

RegexOptions to JavaScript flags mapping

For the options that do have a JavaScript equivalent, the mapping is direct enough to use as a quick lookup while porting a pattern in either direction.

  • RegexOptions.IgnoreCase maps to flag i
  • RegexOptions.Multiline maps to flag m
  • RegexOptions.Singleline (dot matches newline) maps to flag s (dotAll)
  • RegexOptions.IgnorePatternWhitespace has no JavaScript equivalent; write the pattern compactly instead
  • RegexOptions.ECMAScript restricts .NET's own engine to a JavaScript-compatible subset, which is, notably, the closest official acknowledgment from .NET itself that the two flavors normally diverge
  • RegexOptions.Compiled and RegexOptions.CultureInvariant affect performance and locale handling respectively, with no JavaScript flag equivalent since they are not part of matching semantics
  • RegexOptions.RightToLeft has no JavaScript equivalent; there is no way to reverse the scan direction in RegExp

Common .NET regex gotchas

Most .NET-specific regex bugs come from the surrounding C# code rather than the pattern itself.

  • Forgetting the @ verbatim-string prefix, so a pattern like "\\d+" needs doubled backslashes in a normal string literal where @"\d+" would not
  • Using Regex.IsMatch when the code actually needs the matched groups from Regex.Match or Regex.Matches, or vice versa, calling Matches when only a boolean check was needed
  • Not setting a matchTimeout on a Regex built from a pattern that (even partly) originates from user input, leaving the application exposed to a catastrophic-backtracking denial of service
  • Assuming (?<name>...) and (?'name'...) are interchangeable everywhere; they are equivalent in .NET but only the angle-bracket form is portable to other regex flavors, including JavaScript

Frequently asked questions

Does this test the actual .NET regex engine?

No. It runs JavaScript's RegExp engine in your browser, which shares most syntax and behavior with .NET's Regex class, including named groups, but does not support .NET-only features like balancing groups, RightToLeft matching or IgnorePatternWhitespace.

Can I test C# regular expressions here?

Yes for the large majority of patterns, since C# regex uses the .NET regex engine and most everyday syntax, character classes, quantifiers, groups, alternation, anchors, behaves the same way in JavaScript. Strip any @ verbatim-string prefix before pasting, since that is a C# source detail, not part of the pattern.

Does this support .NET balancing groups?

No. Balancing groups, written as (?<name1-name2>...), are unique to the .NET regex engine and used to match nested or balanced constructs. JavaScript's RegExp has no equivalent syntax, so a pattern using them cannot be entered here as-is.

How do I set a regex timeout in .NET?

Pass a TimeSpan as the matchTimeout parameter to the Regex constructor (or the static Regex.Match overload that accepts one). Browser JavaScript has no built-in equivalent, so a runaway pattern tested here can make the tab unresponsive instead of raising a clean RegexMatchTimeoutException.

What is the difference between .NET regex and JavaScript regex?

They overlap closely on core syntax and even share the (?<name>...) named-group form, but .NET adds balancing groups, a configurable match timeout, right-to-left matching and an IgnorePatternWhitespace verbose mode, none of which JavaScript's RegExp supports natively.

Do I need to escape backslashes differently for .NET vs JavaScript?

The regex pattern itself uses the same backslash escapes in both. The difference is entirely in how each language's string literal handles backslashes before the regex engine ever sees them: a C# verbatim string (@"...") avoids double-escaping the way a JavaScript regex literal (/.../.) also does, while a plain C# string requires doubled backslashes.

Can I test RegexOptions.RightToLeft matching on this page?

No, since JavaScript's RegExp has no right-to-left scanning mode. Test that specific behavior directly in .NET; everything else about the pattern can still be validated here first.

Is (?'name'...) the same as (?<name>...) in .NET?

Yes,NET treats the single-quote and angle-bracket named-group syntax as equivalent. Only the angle-bracket form is portable outside .NET though, so rewrite (?'name'...) as (?<name>...) to test the pattern here or reuse it in JavaScript.

More text tools