Capturing a repetition group is always returning the last element but that is not quite helpfull. For example:
var regex = new RegEx("^(?<somea>a)+$");
var match = regex.Match("aaa");
match.Group["somea"]; // return "a"
I would like to have a collection of match element instead of the last match item.
Is that possible?
CaptureCollection
You can use
CaptureCollectionwhich represents the set ofcapturesmade by a single capturing group.If a
quantifieris not applied to a capturing group, theCaptureCollectionincludes a singleCaptureobject that represents the same captured substring as the Group object.If a
quantifieris applied to a capturing group, theCaptureCollectionincludes one Capture object for each captured substring, and theGroupobject provides information only about the last captured substring.So you can do this