Note that there are some explanatory texts on larger screens.

plurals
  1. POIn .NET's RegEx can I get a Groups collection from a Capture object?
    primarykey
    data
    text
    <p>.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 <em>subgroups</em> and I'm trying to get at the subgroups as they are related under the group, and can't find a way. Any suggestions?</p> <p>I've looked at number of other questions, e.g.:</p> <ul> <li><a href="https://stackoverflow.com/questions/7187909/select-multiple-elements-in-a-regular-expression">Select multiple elements in a regular expression</a></li> <li><a href="https://stackoverflow.com/questions/5922683/regex-net-attached-named-group">Regex .NET attached named group</a></li> <li><a href="https://stackoverflow.com/questions/5930952/how-can-i-get-the-regex-groups-for-a-given-capture">How can I get the Regex Groups for a given Capture?</a></li> </ul> <p>but I have found no applicable answer either affirmative ("Yep, here's how") or negative ("Nope, can't be done.").</p> <p>For a contrived example say I have an input string:</p> <pre><code>abc d x 1 2 x 3 x 5 6 e fgh </code></pre> <p>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.</p> <p>So I'm parsing it using this regular expression pattern:</p> <pre><code>.*d (?&lt;x&gt;x ((?&lt;fir&gt;\d+) )?((?&lt;sec&gt;\d+) )?)*?e.* </code></pre> <p>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:</p> <ul> <li>1, 2</li> <li>3</li> <li>5, 6</li> </ul> <p>but how can I get them? I could do the following (in C#): </p> <pre><code>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 (?&lt;x&gt;x ((?&lt;fir&gt;\d+) )?((?&lt;sec&gt;\d+) )?)*?e.*"; foreach (var x in Regex.Match(input, pattern).Groups["x"].Captures) { MessageBox.Show(x.ToString()); } </code></pre> <p>and since I'm referencing group "x" I get these strings:</p> <ul> <li>x 1 2</li> <li>x 3</li> <li>x 5 6</li> </ul> <p>But that doesn't get me at the numbers themselves. So I could do "fir" and "sec" independently instead of just "x": </p> <pre><code>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 (?&lt;x&gt;x ((?&lt;fir&gt;\d+) )?((?&lt;sec&gt;\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()); } </code></pre> <p>to get:</p> <ul> <li>1</li> <li>3</li> <li>5</li> <li>2</li> <li>6</li> </ul> <p>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.</p> <p>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.</p> <p>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.</p> <p>Thoughts?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload