Skip to content
Tutorials Center
My learning

Topics

Lesson 33 of 182 ยท Strings

Regular Expressions

Learn C# regular expressions with System.Text.RegularExpressions - IsMatch, Match, Matches, Replace, Split, named groups, options, GeneratedRegex and how to avoid catastrophic backtracking.

A regular expression (regex) is a tiny pattern language for describing text: "one or more digits", "a word followed by a comma", "an email-shaped thing". Instead of writing a loop that walks a string character by character, you describe the shape you want and let the regex engine find it. In C# the engine lives in System.Text.RegularExpressions, and its main type is Regex.

Regexes are excellent for validating formats, extracting pieces of semi-structured text (log lines, dates, IDs) and rewriting text in bulk. They are also easy to overuse: a pattern is code that is hard to read, hard to debug and can be slow in surprising ways. This lesson teaches the pattern syntax you need, the Regex methods, groups, options, the [GeneratedRegex] source generator, and the backtracking trap that every professional should know about. It follows Working with Strings and Raw, Verbatim and UTF-8 String Literals.

Your first matches

The simplest call is the static Regex.IsMatch(input, pattern), which returns true when the pattern occurs anywhere in the input. Patterns are almost always written as verbatim strings (@"...") or raw string literals, because regex uses a lot of backslashes: in a normal string you would have to write "\\d+" instead of @"\d+".

The building blocks:

Pattern Meaning
abc the literal text abc
. any one character except a newline
\d \w \s a digit, a "word" character (letter, digit, underscore), whitespace
\D \W \S the opposites of the three above
[abc] [a-z] [^0-9] one character from a set, a range, or anything not in the set
* + ? zero or more, one or more, zero or one of the previous item
{2,5} exactly 3, or between 2 and 5 of the previous item
^ $ start and end of the input (or of a line with Multiline)
\b a word boundary (between \w and \W)
a|b a or b
( ) a group: apply a quantifier to several items, and capture the text
C# IsMatch, Match and Matches

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.

Two details to remember. A Match that fails is not null; it is a special object whose Success is false and whose Value is an empty string. And IsMatch without anchors is a "contains" test, so a validation like "the input is all digits" needs ^ and $ (for a whole-string check use \A and \z; plain $ also allows one trailing newline).

Quantifiers: greedy and lazy

*, + and {n,m} are greedy: they grab as much as they can and give characters back only if the rest of the pattern needs them. Add ? after them to make them lazy, matching as little as possible. The difference shows up as soon as a pattern has a flexible part followed by a fixed ending:

C# Greedy versus lazy

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.

The negated set [^>]+ is usually the best choice: it is clear, fast, and can't run past the character you wanted to stop at. (Real HTML is not a job for regex; use an HTML parser. This is just a handy example of greed.)

Groups and named groups

Parentheses do two jobs: they group items so a quantifier can apply to all of them, and they capture the matched text. After a match, match.Groups[0] is the whole match and Groups[1], Groups[2] ... are the captures, numbered by their opening parenthesis from left to right. Naming a group with (?<name>...) is far more readable than counting:

C# Capturing groups and named groups

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.

Use non-capturing groups (?:...) when you only need grouping; they are slightly faster and keep the group numbers tidy. With the option RegexOptions.ExplicitCapture, plain parentheses stop capturing and only named groups do, which is a good habit for larger patterns.

A group inside a repeated group keeps every repetition in group.Captures, while Group.Value holds only the last one. That is rarely needed, but it is how you pull out all items of a repeated structure such as (\d+,)*.

Replace and Split

Regex.Replace(input, pattern, replacement) rewrites every match. The replacement string can refer to captures: $1 or ${name} insert a group, and $& the whole match (write $$ for a literal dollar sign). When the replacement needs logic, pass a MatchEvaluator, a method or lambda that receives each Match and returns the new text. Regex.Split splits a string on a pattern rather than on a fixed delimiter.

C# Replace and Split

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.

The last two lines are a common bug in miniature. The . in 1.5 is a wildcard, so the unescaped pattern also matched 1x5. Whenever part of a pattern comes from user input, pass it through Regex.Escape first, or better, ask yourself whether string.Replace is enough. For plain, fixed text it is simpler and faster.

Options

The RegexOptions enum changes how a pattern behaves. Combine options with |. The ones you will use most:

Option Effect
IgnoreCase Letter case doesn't matter. Add CultureInvariant so the rules don't depend on the current culture.
Multiline ^ and $ match at the start and end of every line, not only of the whole input.
Singleline . also matches \n, so .* can span lines.
IgnorePatternWhitespace Whitespace in the pattern is ignored and # starts a comment, so you can lay a long pattern out over several documented lines.
ExplicitCapture Only named groups capture.
Compiled Compile the pattern to IL at construction for faster matching (costs startup time).
NonBacktracking Use an engine that guarantees linear time (.NET 7 and later; no backreferences or lookarounds).
C# Options in action

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.

Reusing patterns: instances, GeneratedRegex and spans

The static helpers like Regex.IsMatch(input, pattern) look up a small internal cache of Regex objects, which is fine for occasional use. In code that runs many times, create the Regex once and keep it in a static readonly field; constructing a regex parses the pattern every time.

Since .NET 7 there is an even better way: the regex source generator. You declare a partial method with the [GeneratedRegex] attribute, and the compiler generates the matching code at build time, optimized for your pattern. It is faster at startup than RegexOptions.Compiled, works with trimming and AOT, and lets you read and even debug the generated C#. It works on partial methods (and, with C# 13 and .NET 9, on partial properties).

This is a read-only example: it needs the source generator that ships with the .NET SDK build, which our in-browser compiler doesn't run (it fails with error CS8795: Partial method ... must have an implementation part).

C#
// Read-only: shows a project built with the dotnet CLI, not runnable in this editor.
using System.Text.RegularExpressions;

public static partial class Validators
{
    [GeneratedRegex(@"^[A-Z]{2}\d{6}$", RegexOptions.CultureInvariant, matchTimeoutMilliseconds: 500)]
    private static partial Regex PassportNumber();

    public static bool IsPassportNumber(string text) => PassportNumber().IsMatch(text);
}

The runnable equivalent is a cached instance with RegexOptions.Compiled. Another modern tool is EnumerateMatches, which scans a ReadOnlySpan<char> and yields ValueMatch structs with no allocation per match; you slice the original text yourself:

C# A cached Regex and allocation-free matching

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.

Catastrophic backtracking and timeouts

The default engine is a backtracking engine. When a pattern can match in several ways, it tries one, and if the rest fails it goes back and tries another. For ordinary patterns that is fast. For patterns with nested quantifiers over overlapping alternatives, such as (a+)+$, the number of ways to split the input grows exponentially. On an input that almost matches (lots of a, then a !), the engine explores every split before it gives up. Adding one character can double the time.

This is not only a performance bug: it is a security hole called ReDoS (regular expression denial of service). If a user can submit the text that your regex matches, a crafted string can pin a CPU core for minutes.

Defenses, in order of preference:

  1. Write unambiguous patterns. Avoid nested quantifiers on the same characters, prefer negated sets ([^"]*) over .*, and anchor patterns.
  2. Set a match timeout. Pass a TimeSpan to the constructor (or matchTimeoutMilliseconds to [GeneratedRegex]). If a match takes longer, the engine throws RegexMatchTimeoutException.
  3. Use RegexOptions.NonBacktracking for patterns on untrusted input that don't need backreferences or lookarounds. It guarantees time linear in the input length.
  4. Limit the input length before matching.
C# Taming a catastrophic pattern

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.

You can also set an application-wide default timeout with AppContext.SetData("REGEX_DEFAULT_MATCH_TIMEOUT", TimeSpan.FromSeconds(1)) at startup, so no regex without an explicit timeout can run forever.

When to use regex and when to avoid it

Use a regex for pattern-shaped text: validating an ID format, pulling fields out of log lines, normalizing whitespace, tokenizing simple formats. Don't use one when a simpler tool exists (Contains, StartsWith, Split, int.TryParse), and never for nested or recursive formats such as HTML, JSON, XML or source code. Those have parsers. Two more cautions:

  • Don't validate emails or URLs with a monster pattern. The real rules are complicated. For URLs use Uri.TryCreate; for emails, check for a sensible shape and send a confirmation message.
  • Comment complex patterns (with IgnorePatternWhitespace) and cover them with tests that include the nasty cases: empty input, huge input, near-misses.

You will use these tools for real in Project: Log Analyzer. Related reading: Strings for ordinary searching and Unicode, Rune and Encoding for why \w and \d match more than ASCII.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. What does Regex.Match("abc", @"\d+") return when nothing matches?

    Match never returns null. A failed match is a special Match object with Success set to false and an empty Value.
  2. 2. Which pattern matches ONLY a string made entirely of digits?

    Without anchors, \d+ matches a run of digits anywhere inside the text. ^ and $ force the match to cover the whole input.
  3. 3. How do you refer to a named group called year in a replacement string?

    Replacement strings use $1 for numbered groups and ${name} for named groups.
  4. 4. What is the best protection against a regex that could be exploited with catastrophic backtracking?

    A timeout stops runaway matches, an unambiguous pattern avoids them, and NonBacktracking guarantees linear time. The other options don't affect how long matching takes.
  5. 5. Why prefer a static readonly Regex field or [GeneratedRegex] over calling new Regex(pattern) inside a hot method?

    Parsing and preparing a pattern is the expensive part. Create it once and reuse it; the Regex type is thread-safe for matching.

Summary

  • Regex.IsMatch, Match, Matches, Replace and Split cover almost every need. A failed Match is never null; check Success.
  • Write patterns as verbatim (@"...") or raw strings. Anchors (^, $) turn "contains" into "is".
  • Parentheses group and capture. Prefer named groups (?<name>...); use (?:...) when you don't need the capture.
  • Quantifiers are greedy by default; ? makes them lazy, and a negated set is often better than both.
  • RegexOptions changes behavior (IgnoreCase, Multiline, Singleline, IgnorePatternWhitespace, NonBacktracking). Add CultureInvariant with IgnoreCase.
  • Reuse patterns: a static readonly Regex, or [GeneratedRegex] in real projects (the source generator doesn't run in our editor).
  • Nested quantifiers can backtrack exponentially. Defend with timeouts, unambiguous patterns, input limits or NonBacktracking, and escape user text with Regex.Escape.

Next: Methods