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 601277
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T16:41:50+00:00 2026-05-13T16:41:50+00:00

So if I write a regex it’s matches I can get the match or

  • 0

So if I write a regex it’s matches I can get the match or I can access its groups. This seems counter intuitive since the groups are defined in the expression with braces “(” and “)”. It seems like it is not only wrong but redundant. Any one know why?

Regex quickCheck = new Regex(@"(\D+)\d+");
string source = "abc123";

m.Value        //Equals source
m.Groups.Count //Equals 2
m.Groups[0])   //Equals source
m.Groups[1])   //Equals "abc"
  • 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-13T16:41:50+00:00Added an answer on May 13, 2026 at 4:41 pm

    I agree – it is a little strange, however I think there are good reasons for it.

    A Regex Match is itself a Group, which in turn is a Capture.

    But the Match.Value (or Capture.Value as it actually is) is only valid when one match is present in the string – if you’re matching multiple instances of a pattern, then by definition it can’t return everything. In effect – the Value property on the Match is a convenience for when there is only match.

    But to clarify where this behaviour of passing the whole match into Groups[0] makes sense – consider this (contrived) example of a naive code unminifier:

    [TestMethod]
    public void UnMinifyExample()
    {
      string toUnMinify = "{int somevalue = 0; /*init the value*/} /* end */";
      string result = Regex.Replace(toUnMinify, @"(;|})\s*(/\*[^*]*?\*/)?\s*", "$0\n");
      Assert.AreEqual("{int somevalue = 0; /*init the value*/\n} /* end */\n", result);
    }
    

    The regex match will preserve /* */ comments at the end of a statement, placing a newline afterwards – but works for either ; or } line-endings.

    Okay – you might wonder why you’d bother doing this with a regex – but humour me 🙂

    If Groups[0] generated by the matches for this regex was not the whole capture – then a single-call replace would not be possible – and your question would probably be asking why doesn’t the whole match get put into Groups[0] instead of the other way round!

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

Sidebar

Related Questions

I want to write a regex that matches if a string contains XYZ but
How do I write a .net regex which will match a string that does
Wanna write a RegEx to validate a driving license. if it doesn't start with
I'm trying to write a regex function that will identify and replace a single
I'm trying to write a regex that will parse out the directory and filename
I want to write a piece of software which is essentially a regex data
I'm trying to write a regular expression that validates a date. The regex needs
You write a function and, looking at the resulting assembly, you see it can
I need to solve the following question which i can't get to work by
Ok I give up, I've been trying to write a regexp in ant to

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.