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

  • Home
  • SEARCH
  • 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 609777
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T17:36:10+00:00 2026-05-13T17:36:10+00:00

I am making a small applicaiton using .NET Regex types. And the Capture, Group

  • 0

I am making a small applicaiton using .NET Regex types. And the “Capture, Group and Match” types totally confused me. I have never seen such an ugly solution. Could someone explain their usage for me? Many thanks.

  • 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-13T17:36:10+00:00Added an answer on May 13, 2026 at 5:36 pm

    Here’s a simpler example than the one in the document @Dav cited:

    string s0 = @"foo%123%456%789";
    Regex r0 = new Regex(@"^([a-z]+)(?:%([0-9]+))+$");
    Match m0 = r0.Match(s0);
    if (m0.Success)
    {
      Console.WriteLine(@"full match: {0}", m0.Value);
      Console.WriteLine(@"group #1: {0}", m0.Groups[1].Value);
      Console.WriteLine(@"group #2: {0}", m0.Groups[2].Value);
      Console.WriteLine(@"group #2 captures: {0}, {1}, {2}",
                        m0.Groups[2].Captures[0].Value,
                        m0.Groups[2].Captures[1].Value,
                        m0.Groups[2].Captures[2].Value);
    }
    

    result:

    full match: foo%123%456%789
    group #1: foo
    group #2: 789
    group #2 captures: 123, 456, 789

    The full match and group #1 results are straightforward, but the others require some explanation. Group #2, as you can see, is inside a non-capturing group that’s controlled by a + quantifier. It matches three times, but if you request its Value, you only get what it matched the third time around–the final capture. Similarly, if you use the $2 placeholder in a replacement string, the final capture is what gets inserted in its place.

    In most regex flavors, that’s all you can get; each intermediate capture is overwritten by the next and lost; .NET is almost unique in preserving all of the captures and making them available after the match is performed. You can access them directly as I did here, or iterate through the CaptureCollection as you would a MatchCollection. There’s no equivalent for the $1-style replacement-string placeholders, though.

    So the reason the API design is so ugly (as you put it) is twofold: first it was adapted from Perl’s integral regex support to .NET’s object-oriented framework; then the CaptureCollection structure was grafted onto it. Perl 6 offers a much cleaner solution, but the authors accomplished that by rewriting Perl practically from scratch and throwing backward compatibility out the window.

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

Sidebar

Ask A Question

Stats

  • Questions 357k
  • Answers 357k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer The other answers are correct. Here is some code you… May 14, 2026 at 9:40 am
  • Editorial Team
    Editorial Team added an answer you ruin the noConflict concept by reassigning the jquery to… May 14, 2026 at 9:40 am
  • Editorial Team
    Editorial Team added an answer If you get that particular error, you don't actually have… May 14, 2026 at 9:40 am

Related Questions

No related questions found

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.