开发者

Get found items as list from regex

开发者 https://www.devze.com 2023-01-21 10:29 出处:网络
Given the foll开发者_运维技巧owing code: var myList = new List<string> { \"red\", \"blue\", \"green\" };

Given the foll开发者_运维技巧owing code:

var myList = new List<string> { "red", "blue", "green" };
Regex r = new Regex("\\b(" + string.Join("|", myList.ToArray()) + ")\\b");
MatchCollection m = r.Matches("Alfred has a red and blue tie and blue pants.");

Is there a way to derive a List<string> of the "found" items ("red", "blue", "blue")?


var n = (from Match match in m
         select match.Value).ToList()
0

精彩评论

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