我有一个小测试程序,它构建了一个List不同的字符串,所有字符串都包含相同的格式化数字.然后我还声明了另一个列表,该列表应该包含前一个列表中每个字符串的特定数字.
我的计划是通过在lambda函数内部使用正则表达式匹配来实现这一点.
每次我尝试这样做我都会收到以下错误:
List<string> newList = new List<string>(new string[] { "MyName - v 3.7.5.0 ... CPU:",
"MyName - v ... CPU: - 1.5.7.2",
"4.21.66.2 - v ... CPU:",
" - v ... CPU: 31.522.9.0" });
Regex match = new Regex("(\\d+\\.)+\\d");
List<string> otherList = newList.FindAll(str => match.Match(str).Value);
Run Code Online (Sandbox Code Playgroud)
有什么方法可以使用lambda函数来实现这个目的吗?
你可以试试这个:
List<string> otherList = newList.Select(str => match.Match(str).Value).ToList();
Run Code Online (Sandbox Code Playgroud)
顺便说一句,你的代码失败了,因为谓词是期待的bool.