Is it possible to convert a GroupCollection
to a List
or an IEnumerable
?
I'm referring to the GroupCollection
in regular expressions.
Sure
GroupCollection col = ...;
IEnumerable<Group> enumerable = col.Cast<Group>();
List<Group> list = col.Cast<Group>().ToList();
Here's one-liner version:
new Regex("[your regex goes here]").Matches(stringThatYouAreTryingToExtractGroupsFrom)[0].Groups.Cast<Group>().Skip(1).Where(o => o.Value != "").Select(o => o.Value)
This will throw
an exception if there are no matches. I am also skipping the original [0]
group that captures full regex and filtering out empty groups.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With