Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/333.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 从正则表达式中以列表形式获取找到的项_C#_Asp.net_Regex_List - Fatal编程技术网

C# 从正则表达式中以列表形式获取找到的项

C# 从正则表达式中以列表形式获取找到的项,c#,asp.net,regex,list,C#,Asp.net,Regex,List,给定以下代码: 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."); var myList=新列表{“红色”、“蓝

给定以下代码:

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.");
var myList=新列表{“红色”、“蓝色”、“绿色”};
Regex r=newregex(“\\b(“+string.Join(“|”),myList.ToArray())+”\\b”);
MatchCollection m=r.Matches(“阿尔弗雷德有一条红蓝相间的领带和蓝色的裤子。”);

有没有办法导出“找到”项(“红色”、“蓝色”、“蓝色”)的
列表
。我使用的是foreach(m中的var-match)。当我把它改为foreach(m中的Match)时,它就可以工作了。谢谢因为m不是improvement IEnumerable,所以需要明确地告诉编译器您正在处理匹配对象。假设您的var是一个对象
var n = (from Match match in m
         select match.Value).ToList()