.NET
regular expressions
groups
captures
programming differences

What's the difference between groups and captures in .NET regular expressions?

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

Introduction

In .NET regular expressions, a group and a capture are related, but they are not the same thing. A group is the logical part of the pattern defined by parentheses, while captures are the individual pieces of text that group matched during the overall regex evaluation.

Think of a Group as the Pattern Container

A capturing group is created with parentheses.

csharp
1using System.Text.RegularExpressions;
2
3var match = Regex.Match("2026-03-11", @"(\d{4})-(\d{2})-(\d{2})");
4
5Console.WriteLine(match.Groups[1].Value); // 2026
6Console.WriteLine(match.Groups[2].Value); // 03
7Console.WriteLine(match.Groups[3].Value); // 11

Each group corresponds to one parenthesized part of the pattern. Groups are how you structure and reference meaningful parts of the match.

A Capture Is One Actual Match of That Group

If a group is repeated, it can capture multiple times during one overall match. The Group object then exposes all of those captures through its Captures collection.

csharp
1using System.Text.RegularExpressions;
2
3var match = Regex.Match("abc", @"(\w)+");
4var group = match.Groups[1];
5
6Console.WriteLine(group.Value);              // c
7Console.WriteLine(group.Captures.Count);     // 3
8
9foreach (Capture capture in group.Captures)
10{
11    Console.WriteLine(capture.Value);        // a, b, c
12}

This is the key difference: one group, many captures.

Why Group.Value Can Be Misleading

Developers often expect Group.Value to contain everything the repeated group matched. In .NET, for repeated capturing groups, Group.Value usually reflects the last capture, while Group.Captures stores the full history of what that group matched along the way.

That behavior is what makes the distinction between groups and captures important in real code.

Named Groups Still Have Captures

Named groups behave the same way conceptually. They are easier to read, but the capture behavior does not change.

csharp
1using System.Text.RegularExpressions;
2
3var match = Regex.Match("12-34-56", @"(?<part>\d{2})-?(?<part>\d{2})-?(?<part>\d{2})");
4var group = match.Groups["part"];
5
6Console.WriteLine(group.Value);
7Console.WriteLine(group.Captures.Count);

If the same named group participates multiple times, its captures collection becomes especially useful.

When You Need Non-Capturing Groups Instead

Sometimes you only need grouping for precedence or repetition and do not want a capturing group at all. In that case, use a non-capturing group with (?:...).

csharp
var match = Regex.Match("catcat", @"(?:cat)+");

This avoids creating extra groups when you do not need to inspect the matched subparts.

Practical Rule of Thumb

Use this mental model:

  • group = the regex subpattern
  • capture = one concrete text match produced by that group

That makes it easier to understand why a single group can have many captures but still appear only once in Groups.

This Matters Most with Repetition

If the group is not repeated, the difference between group and capture is easy to miss because there is often only one capture. Repetition is where the distinction becomes operationally important.

That is why this topic shows up most often when debugging quantified groups such as (...)+ or (...)*.

That is the usual teaching example.

It is worth remembering.

For debugging.

Common Pitfalls

  • Assuming groups and captures are interchangeable terms.
  • Reading Group.Value and expecting it to contain every repeated capture.
  • Forgetting about Group.Captures when debugging repeated group behavior.
  • Using capturing groups when a non-capturing group would be cleaner.
  • Misreading named groups as if they changed the underlying capture model.

Summary

  • A group is the parenthesized regex subpattern.
  • A capture is one actual piece of text matched by that group.
  • Repeated groups can produce multiple captures during one match.
  • 'Group.Value and Group.Captures serve different purposes.'
  • Use non-capturing groups when you only need grouping, not capture data.

Related reading
Course
Intermediate
27 lessons
14 hours
OOD Fundamentals

Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.

View the course
Track 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.

Browse interview questions

All Rights Reserved.