1
Vote

Display Captures as well as groups in the output

description

The following code:
     Regex regex = new Regex("B(?:(\\d+)(?:,?))*E");
     foreach (Match match in regex.Matches("B12,234EB12EBEB12,13,14E"))
     {
        Console.WriteLine("Match {0}", match.Value);
        foreach (Group group in match.Groups)
        {
           Console.WriteLine("...Group {0}", group.Value);
           foreach (Capture capture in group.Captures)
           {
              Console.WriteLine("......Capture {0}", capture.Value);
           }
        }
     }
displays the following output:
Match B12,234E
...Group B12,234E
......Capture B12,234E
...Group 234
......Capture 12
......Capture 234
Match B12E
...Group B12E
......Capture B12E
...Group 12
......Capture 12
Match BE
...Group BE
......Capture BE
...Group
Match B12,13,14E
...Group B12,13,14E
......Capture B12,13,14E
...Group 14
......Capture 12
......Capture 13
......Capture 14
It would be good if RegEx Editor also showed the Captures.

comments

mgbrown wrote Jun 2, 2014 at 1:31 PM

Note nested groups don't produce nested captures in the object model.

i.e there is no Match.Groups[I].Groups property. As such it would be impossible to know which parent group the child belonged to.