How do I access named capturing groups in a .NET Regex?
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
In .NET, named capturing groups use the syntax (?<name>pattern) in a regex pattern, and you access them through match.Groups["name"] on the Match object. Named groups make regex code more readable than numbered groups (\1, \2) because you reference matches by descriptive names instead of position indices. The System.Text.RegularExpressions.Regex class supports named groups in Match(), Matches(), and Replace() operations.
Defining Named Groups
Use (?<name>...) to define a named capturing group:
Alternative Syntax
.NET also supports (?'name'...) with single quotes:
Accessing Groups by Name vs Index
Named groups are also accessible by their numeric index:
Iterating Over Multiple Matches
Named Groups in Replace
Reference named groups in replacement strings with ${name}:
Named Groups with LINQ
Checking if a Group Matched
A named group may not participate in every match (e.g., optional groups):
Named Groups with Repeated Captures
When a named group matches multiple times (inside a quantifier), .Captures contains all matches:
Compiled Regex for Performance
For frequently used patterns, compile the regex:
.NET 7+ Source Generators
Source-generated regex provides the best performance with zero runtime compilation overhead.
Common Pitfalls
- Accessing a group that does not exist:
match.Groups["nonexistent"]returns an emptyGroupobject (not null) withSuccess = falseandValue = "". Always check.Successbefore using the value to avoid silent empty-string bugs. - Confusing
.Valuewith.Captures: For a group inside a quantifier (e.g.,(?<word>\w+)+),.Valuereturns only the last capture. Use.Capturesto access all individual matches of that group. - Forgetting
${}syntax inReplace: In replacement strings, named groups use${name}, not $nameor\name. Using the wrong syntax inserts a literal string instead of the captured value. - Not escaping special regex characters in group patterns: Characters like
.,(,),[,+have special meaning in regex. Escape them with\(or\\in C# strings) or useRegex.Escape()for dynamic patterns. - Performance with uncompiled regex in loops: Creating a new
Regexobject inside a loop recompiles the pattern each time. UseRegexOptions.Compiled, a static field, or[GeneratedRegex](.NET 7+) for patterns used repeatedly.
Summary
- Define named groups with
(?<name>pattern)and access withmatch.Groups["name"].Value - Use
${name}in replacement strings forRegex.Replace() - Check
match.Groups["name"].Successfor optional groups that may not match - Use
.Capturesinstead of.Valuewhen a named group matches multiple times inside a quantifier - Use
RegexOptions.Compiledor[GeneratedRegex](.NET 7+) for performance-critical patterns
Related reading
- How do I add an existing directory tree to a project in Visual Studio?
- How do I add an existing Solution to GitHub from Visual Studio 2013
- How do I allow assembly unit testing one to access internal properties of another assembly?
- How do I apply OrderBy on an IQueryable using a string column name within a generic extension method?
- How do I arrange flow control in TPL Dataflows?
- How do I Async download multiple files using webclient, but one at a time?
- How do I attach Visual Studio to a process that is not started yet?
- How do I automatically delete temp files in C?

OOD Fundamentals
Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.