Reza Ahmadi Reza Ahmadi - 1 year ago 404
C# Question

Regex: How to capture all iterations in repeated capturing group

I would expect these lines of C#:

var regex = new Regex("A(bC*)*");
var match = regex.Match("AbCCbbCbCCCCbbb");
var groups = match.Groups;

to return something like:

["AbCCbbCbCCCCbbb", "A", "bCC", "b", "bC", "bCCC", "b", "b", "b"]

but instead it returns only the last captured match:

["AbCCbbCbCCCCbbb", "b"]

Here Regex101 also displays the following as a warning:

A repeated capturing group will only capture the last iteration. Put a capturing group around the repeated group to capture all iterations or use a non-capturing group instead if you're not interested in the data

How should I change my regex pattern?

Answer Source

If you want to also capture A, just wrap it with parentheses: new Regex("(A)(bC*)*"). See the regex demo.

enter image description here

Then, collect all the values you have got inside CaptureCollection:

var regex = new Regex("(A)(bC*)*");
var match = regex.Matches("AbCCbbCbCCCCbbb")
     .SelectMany(x => x.Groups.Cast<Group>()
          .SelectMany(v => v.Captures
              .Select(t => t.Value)
 foreach (var s in match)

See the C# demo

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download