Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 942307
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T22:15:04+00:00 2026-05-15T22:15:04+00:00

I’m a little fuzzy on what the difference between a "group" and a "capture"

  • 0

I’m a little fuzzy on what the difference between a "group" and a "capture" are when it comes to .NET’s regular expression language. Consider the following C# code:

MatchCollection matches = Regex.Matches("{Q}", @"^\{([A-Z])\}$");

I expect this to result in a single capture for the letter ‘Q’, but if I print the properties of the returned MatchCollection, I see:

matches.Count: 1
matches[0].Value: {Q}
matches[0].Captures.Count: 1
matches[0].Captures[0].Value: {Q}
matches[0].Groups.Count: 2
matches[0].Groups[0].Value: {Q}
matches[0].Groups[0].Captures.Count: 1
matches[0].Groups[0].Captures[0].Value: {Q}
matches[0].Groups[1].Value: Q
matches[0].Groups[1].Captures.Count: 1
matches[0].Groups[1].Captures[0].Value: Q

What exactly is going on here? I understand that there’s also a capture for the entire match, but how do the groups come in? And why doesn’t matches[0].Captures include the capture for the letter ‘Q’?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T22:15:04+00:00Added an answer on May 15, 2026 at 10:15 pm

    You won’t be the first who’s fuzzy about it. Here’s what the famous Jeffrey Friedl has to say about it (pages 437+):

    Depending on your view, it either adds
    an interesting new dimension to the
    match results, or adds confusion and
    bloat.

    And further on:

    The main difference between a Group
    object and a Capture object is that
    each Group object contains a
    collection of Captures representing
    all the intermediary matches by the
    group during the match, as well as the
    final text matched by the group.

    And a few pages later, this is his conclusion:

    After getting past the .NET
    documentation and actually
    understanding what these objects add,
    I’ve got mixed feelings about them. On
    one hand, it’s an interesting
    innovation [..] on the other hand, it
    seems to add an efficiency burden [..]
    of a functionality that won’t be used
    in the majority of cases

    In other words: they are very similar, but occasionally and as it happens, you’ll find a use for them. Before you grow another grey beard, you may even get fond of the Captures…


    Since neither the above, nor what’s said in the other post really seems to answer your question, consider the following. Think of Captures as a kind of history tracker. When the regex makes his match, it goes through the string from left to right (ignoring backtracking for a moment) and when it encounters a matching capturing parentheses, it will store that in $x (x being any digit), let’s say $1.

    Normal regex engines, when the capturing parentheses are to be repeated, will throw away the current $1 and will replace it with the new value. Not .NET, which will keep this history and places it in Captures[0].

    If we change your regex to look as follows:

    MatchCollection matches = Regex.Matches("{Q}{R}{S}", @"(\{[A-Z]\})+");
    

    you will notice that the first Group will have one Captures (the first group always being the whole match, i.e., equal to $0) and the second group will hold {S}, i.e. only the last matching group. However, and here’s the catch, if you want to find the other two catches, they’re in Captures, which contains all intermediary captures for {Q} {R} and {S}.

    If you ever wondered how you could get from the multiple-capture, which only shows last match to the individual captures that are clearly there in the string, you must use Captures.

    A final word on your final question: the total match always has one total Capture, don’t mix that with the individual Groups. Captures are only interesting inside groups.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.