开发者

Is it possible to convert GroupCollection to List or IEnumerable?

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

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜