Skip to main content

How to Group Without Capturing in a C# Regex

· 5 min read
Jagdish Kumawat
Founder @ Dewiride

Brackets in a regular expression do two jobs: they group part of the pattern, and they capture what that part matched. When you only need the grouping, write (?:...) and nothing is captured.

Microsoft's reference: The following grouping construct does not capture the substring that's matched by a subexpression, and The noncapturing group construct is typically used when a quantifier is applied to a group, but the substrings captured by the group are of no interest. It's also handy for alternatives that belong together, such as two currencies.

Syntax​

Syntax
Regex.Match(<text>, @"...(?:<subexpression>)...")
PartWhat it is
(?: and )Group the part in between without capturing it
<subexpression>Any pattern, such as two alternatives or a part to repeat
A plain ( ) groupCaptures what it matched. Capturing groups are numbered from 1 in Match.Groups, and group 0 is the whole match

The Example​

A console app called NonCapturing reads the amount from Total: 42 EUR twice: once with the currency in a capturing group, and once with it in a non-capturing group.

Prerequisites​

  • The .NET 10 SDK. dotnet --version prints a version that starts with 10.0.

Step 1: Create a Console App​

Terminal
dotnet new console -n NonCapturing
cd NonCapturing
Output
The template "Console App" was created successfully.

Processing post-creation actions...
Restoring /Users/you/source/NonCapturing/NonCapturing.csproj:
Determining projects to restore...
Restored /Users/you/source/NonCapturing/NonCapturing.csproj (in 49 ms).
Restore succeeded.

dotnet new console -n NonCapturing creates a folder named NonCapturing with a project file and a Program.cs, and cd moves into it. Everything this example uses is part of .NET, so there's no package to add. Open the folder in VS Code, or NonCapturing.csproj in Visual Studio.

Step 2: Write the Code​

Replace everything in Program.cs. First the text and the two matches:

Program.cs (the matches)
using System.Text.RegularExpressions;

string line = "Total: 42 EUR";

// (EUR|USD) groups the two currencies and captures the one that matched.
Match capturing = Regex.Match(line, @"(\d+) (EUR|USD)");

// (?:EUR|USD) only groups them: nothing is captured.
Match nonCapturing = Regex.Match(line, @"(\d+) (?:EUR|USD)");
  • (\d+) captures the amount in both patterns.
  • (EUR|USD) groups the two currencies and captures the one that matched.
  • (?:EUR|USD) groups them in the same way but captures nothing.

Then print the groups:

Program.cs (the groups)
Console.WriteLine($"Capturing: {capturing.Groups.Count} groups");
Console.WriteLine($" amount {capturing.Groups[1].Value}, currency {capturing.Groups[2].Value}");
Console.WriteLine($"Non-capturing: {nonCapturing.Groups.Count} groups");
Console.WriteLine($" amount {nonCapturing.Groups[1].Value}");

Step 3: Run It​

Run it in VS Code's terminal, or press Ctrl+F5 in Visual Studio:

Terminal
dotnet run
Output
Capturing: 3 groups
amount 42, currency EUR
Non-capturing: 2 groups
amount 42

macOS-style terminal window titled Terminal — NonCapturing showing dotnet run: the capturing pattern has 3 groups with amount 42 and currency EUR, and the non-capturing pattern has 2 groups with amount 42

The capturing pattern has three groups: the whole match, the amount and the currency. The non-capturing one has two: the whole match and the amount. Both matched the same text, and the amount is group 1 in both.

The Complete File​

macOS-style code window titled Program.cs — NonCapturing showing two Regex.Match calls on Total: 42 EUR, one with the currencies in a capturing group and one with them in a non-capturing group, and lines that print each match's group count and values

Program.cs
using System.Text.RegularExpressions;

string line = "Total: 42 EUR";

// (EUR|USD) groups the two currencies and captures the one that matched.
Match capturing = Regex.Match(line, @"(\d+) (EUR|USD)");

// (?:EUR|USD) only groups them: nothing is captured.
Match nonCapturing = Regex.Match(line, @"(\d+) (?:EUR|USD)");

Console.WriteLine($"Capturing: {capturing.Groups.Count} groups");
Console.WriteLine($" amount {capturing.Groups[1].Value}, currency {capturing.Groups[2].Value}");
Console.WriteLine($"Non-capturing: {nonCapturing.Groups.Count} groups");
Console.WriteLine($" amount {nonCapturing.Groups[1].Value}");

Common Mistakes​

MistakeWhat happensDo this instead
Reading a group that is now non-capturingGroups[2] still answers, but with nothing in it: Success was False and Value was emptyCount only the plain ( ) groups when you number them, or name the groups you read
Using plain brackets only to repeat part of a patternIt works, but it captures something you never readUse (?:...): ^(?:ha)+$ matched hahaha

FAQ​

Can I make every unnamed group non-capturing at once?​

Yes, with RegexOptions.ExplicitCapture. With it, the capturing pattern above had a single group, the whole match. Microsoft's grouping reference notes that with this option, the only way to capture a subexpression is to explicitly name capturing groups.

What does a repeated group match?​

The whole run: Regex.Match("hahaha!", "^(?:ha)+").Value returned hahaha.

Conclusion​

Write (?:...) when you need brackets for grouping or repeating, and plain ( ) only for what you want to read from Match.Groups. That keeps the group numbers short and predictable.

Additional Resources​