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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T00:04:13+00:00 2026-06-16T00:04:13+00:00

.NET offers a Capture collection in its RegularExpression implementation so you can get all

  • 0

.NET offers a Capture collection in its RegularExpression implementation so you can get all instances of a given repeating group rather than just the last instance of it. That’s great, but I have a repeating group with subgroups and I’m trying to get at the subgroups as they are related under the group, and can’t find a way. Any suggestions?

I’ve looked at number of other questions, e.g.:

  • Select multiple elements in a regular expression
  • Regex .NET attached named group
  • How can I get the Regex Groups for a given Capture?

but I have found no applicable answer either affirmative (“Yep, here’s how”) or negative (“Nope, can’t be done.”).

For a contrived example say I have an input string:

abc d x 1 2 x 3 x 5 6 e fgh

where the “abc” and “fgh” represent text that I want to ignore in the larger document, “d” and “e” wrap the area of interest, and within that area of interest, “x n [n]” can repeat any number of times. It’s those number pairs in the “x” areas that I’m interested in.

So I’m parsing it using this regular expression pattern:

.*d (?<x>x ((?<fir>\d+) )?((?<sec>\d+) )?)*?e.*

which will find exactly one match in the document, but capture the “x” group many times. Here are the three pairs I would want to extract in this example:

  • 1, 2
  • 3
  • 5, 6

but how can I get them? I could do the following (in C#):

using System;
using System.Text;
using System.Text.RegularExpressions;

string input = "abc d x 1 2 x 3 x 5 6 e fgh";
string pattern = @".*d (?<x>x ((?<fir>\d+) )?((?<sec>\d+) )?)*?e.*";
foreach (var x in Regex.Match(input, pattern).Groups["x"].Captures) {
    MessageBox.Show(x.ToString());
}

and since I’m referencing group “x” I get these strings:

  • x 1 2
  • x 3
  • x 5 6

But that doesn’t get me at the numbers themselves. So I could do “fir” and “sec” independently instead of just “x”:

using System;
using System.Text;
using System.Text.RegularExpressions;

string input = "abc d x 1 2 x 3 x 5 6 e fgh";
string pattern = @".*d (?<x>x ((?<fir>\d+) )?((?<sec>\d+) )?)*?e.*";
Match m = Regex.Match(input, pattern);
foreach (var f in m.Groups["fir"].Captures) {
    MessageBox.Show(f.ToString());
}

foreach (var s in m.Groups["sec"].Captures) {
    MessageBox.Show(s.ToString());
}

to get:

  • 1
  • 3
  • 5
  • 2
  • 6

but then I have no way of knowing that it’s the second pair that’s missing the “4”, and not one of the other pairs.

So what to do? I know I could easily parse this out in C# or even with a second regex test on the “x” group, but since the first RegEx run has already done all the work and the results ARE known, it seems there ought to be a way to manipulate the Match object to get what I need out of it.

And remember, this is a contrived example, the real world case is somewhat more complex so just throwing extra C# code at it would be a pain. But if the existing .NET objects can’t do it, then I just need to know that and I’ll continue on my way.

Thoughts?

  • 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-06-16T00:04:14+00:00Added an answer on June 16, 2026 at 12:04 am

    I am not aware of a fully build in solution and could not find one after a quick search, but this does not exclude the possibility that there is one.

    My best suggestion is to use the Index and Length properties to find matching captures. It seems not really elegant but you might be able to come up with some quite nice code after writing some extension methods.

    var input = "abc d x 1 2 x 3 x 5 6 e fgh";
    
    var pattern = @".*d (?<x>x ((?<fir>\d+) )?((?<sec>\d+) )?)*?e.*";
    
    var match = Regex.Match(input, pattern);
    
    var xs = match.Groups["x"].Captures.Cast<Capture>();
    
    var firs = match.Groups["fir"].Captures.Cast<Capture>();
    var secs = match.Groups["sec"].Captures.Cast<Capture>();
    
    Func<Capture, Capture, Boolean> test = (inner, outer) =>
        (inner.Index >= outer.Index) &&
        (inner.Index < outer.Index + outer.Length);
    
    var result = xs.Select(x => new
                                {
                                    Fir = firs.FirstOrDefault(f => test(f, x)),
                                    Sec = secs.FirstOrDefault(s => test(s, x))
                                })
                   .ToList();
    

    Here one possible solution using the following extension method.

    internal static class Extensions
    {
        internal static IEnumerable<Capture> GetCapturesInside(this Match match,
             Capture capture, String groupName)
        {
            var start = capture.Index;
            var end = capture.Index + capture.Length;
    
            return match.Groups[groupName]
                        .Captures
                        .Cast<Capture>()
                        .Where(inner => (inner.Index >= start) &&
                                        (inner.Index < end));
        }
    }
    

    Now the you can rewrite the code as follows.

    var input = "abc d x 1 2 x 3 x 5 6 e fgh";
    
    var pattern = @".*d (?<x>x ((?<fir>\d+) )?((?<sec>\d+) )?)*?e.*";
    
    var match = Regex.Match(input, pattern);
    
    foreach (Capture x in match.Groups["x"].Captures)
    {
        var fir = match.GetCapturesInside(x, "fir").SingleOrDefault();
        var sec = match.GetCapturesInside(x, "sec").SingleOrDefault();
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

reversehttp.net offers little immediate insight into what reversehttp truly is and how this can
ASP.NET Offers great caching api with lots of functionality. Can anybody answer me that
I've never looked much into all what .NET offers for user input validation because
Can anyone recommend a .NET winforms control that offers similar functionality to the address
Doing this: @resp = Net::HTTP.get_response(api.something.com, /feed/v1/offers.json?#{@params_api_string}) I get this response in @resp: #<Net::HTTPOK:0x7f451e9d3ef0> How
Is there any API SQL Server 2008 offers for .net application to create and
.Net contains a nice control called DocumentViewer . it also offers a subcontrol for
net website, I would like to add button by which user can view the
.NET offers a generic list container whose performance is almost identical (see Performance of
When I create a ASP.NET MVC web application in Visual Studio 2010 it offers

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.