Skip to main content

How to Use Named Groups in a C# Regex

· 5 min read
Jagdish Kumawat
Founder @ Dewiride

A regular expression can do more than say yes, it matches: it can hand back the parts of the text you care about. Give each part a name in the pattern, and read it back by that name instead of by its position.

Parentheses in a pattern make a group, and .NET keeps the text each group matched. Unnamed groups are numbered 1, 2, 3 from left to right, which breaks the moment you add a group in front. A named group is read by its name, so the code says what it reads, and it keeps working when the pattern grows.

Syntax​

Syntax
using System.Text.RegularExpressions;

Match <match> = Regex.Match(<input>, @"...(?<name>subexpression)...");

if (<match>.Success)
{
string <value> = <match>.Groups["name"].Value;
}
PartWhat it is
Regex.Match(<input>, <pattern>)Finds the first match of <pattern> in <input>
(?<name>subexpression)A named group. subexpression is any pattern, and name is what you call the text it matches. In the pattern you write your own name in place of name
<match>.Successtrue when the pattern matched
<match>.Groups["name"].ValueThe text the named group matched

Microsoft's quick reference describes the construct: Captures the matched subexpression into a named group. And the name has one rule: it must not contain any punctuation characters and cannot begin with a number.

The Example​

A console app called NamedGroups reads the line Order 1042 shipped on 2026-11-26 and prints the order number and the date, each read from a named 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 NamedGroups
cd NamedGroups
Output
The template "Console App" was created successfully.

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

dotnet new console -n NamedGroups creates a folder named NamedGroups with a project file and a Program.cs, and cd moves into it. Regular expressions are part of .NET, so there's no package to add.

Step 2: Write the Code​

Replace everything in Program.cs. First the line and the match:

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

string line = "Order 1042 shipped on 2026-11-26";
Match match = Regex.Match(line, @"Order (?<order>\d+) shipped on (?<date>\d{4}-\d{2}-\d{2})");

The pattern is the line itself, with its two changing parts replaced by named groups:

  • (?<order>\d+) is one or more digits, named order.
  • (?<date>\d{4}-\d{2}-\d{2}) is four digits, a hyphen, two digits, a hyphen and two digits, named date.
  • Order , shipped on and the spaces are plain text that must be there as written.

Then read the groups by name:

Program.cs (the values)
if (match.Success)
{
Console.WriteLine($"Order: {match.Groups["order"].Value}");
Console.WriteLine($"Date: {match.Groups["date"].Value}");
}

match.Success is true only when the line matched. Inside, match.Groups["order"] is the group named order, and its Value is the text it matched.

Step 3: Run It​

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

Terminal
dotnet run
Output
Order: 1042
Date: 2026-11-26

macOS-style terminal window titled Terminal — NamedGroups showing dotnet run printing Order: 1042 and Date: 2026-11-26

The order number and the date came out on their own, each read by its name.

The Complete File​

macOS-style code window titled Program.cs — NamedGroups showing the line Order 1042 shipped on 2026-11-26, Regex.Match with a pattern that has a group named order for the digits and a group named date for the date, and an if block that prints match.Groups of order and of date when the match succeeds

Program.cs
using System.Text.RegularExpressions;

string line = "Order 1042 shipped on 2026-11-26";
Match match = Regex.Match(line, @"Order (?<order>\d+) shipped on (?<date>\d{4}-\d{2}-\d{2})");

if (match.Success)
{
Console.WriteLine($"Order: {match.Groups["order"].Value}");
Console.WriteLine($"Date: {match.Groups["date"].Value}");
}

Common Mistakes​

MistakeWhat happensDo this instead
Misspelling the name, such as match.Groups["ordr"]No error: you get a group with Success False and an empty ValueCopy the name from the pattern
Reading the groups without checking match.SuccessOn Order shipped yesterday, Success was False and Groups["order"].Value was empty, with no errorCheck match.Success first

FAQ​

Can I use a named group in a replacement?​

Yes, as ${name}. With the same pattern, Regex.Replace(line, pattern, "${date}: order #${order}") turned the line into:

Output
2026-11-26: order #1042

Microsoft's guide lists ${name} among the ways to use a named group, in a Regex.Replace or Match.Result method call.

What number does a named group get?​

Named groups are numbered after the unnamed ones. new Regex(pattern).GetGroupNames() listed 0, order, date: group 0 is always the whole match. Microsoft's guide: Named matched subexpressions are numbered consecutively from left to right after matched subexpressions.

How do I find every match, not only the first?​

Use Regex.Matches(<input>, <pattern>). Microsoft's reference: it searches an input string for all occurrences of a regular expression and returns all the matches. Read the same named groups from each Match in a foreach loop.

Conclusion​

Write (?<name>subexpression) in the pattern, check match.Success, and read match.Groups["name"].Value. Here the order number and the date came out of a line of text by name, and the code says which part it reads.

Additional Resources​