开发者

Is it possible to convert GroupCollection to List or IEnumerable?

开发者 https://www.devze.com 2022-12-23 01:03 出处:网络
Is it possible to convert a GroupCollection to a List or an IEnumerab开发者_如何学Gole? I\'m referring to the GroupCollection in regular expressions.Sure

Is it possible to convert a GroupCollection to a List or an IEnumerab开发者_如何学Gole? 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.

0

精彩评论

暂无评论...
验证码 换一张
取 消