C# 将MatchCollection转换为Double数组
我有以下生成MatchCollection的代码:C# 将MatchCollection转换为Double数组,c#,regex,C#,Regex,我有以下生成MatchCollection的代码: var tmp3 = myregex.Matches(text_to_split); tmp3中的匹配项是93.4和-276.2等字符串。我真正需要的是将这个MatchCollection转换成一个double数组。如何做到这一点?您可以使用该方法将字符串转换为双精度: var tmp3 = myregex.Matches(text_to_split); foreach (Match match in tmp3) { double v
var tmp3 = myregex.Matches(text_to_split);
tmp3
中的匹配项是93.4
和-276.2
等字符串。我真正需要的是将这个MatchCollection转换成一个double数组。如何做到这一点?您可以使用该方法将字符串转换为双精度:
var tmp3 = myregex.Matches(text_to_split);
foreach (Match match in tmp3)
{
double value = double.Parse(match.Value);
// TODO : do something with the matches value
}
如果你像我一样是LINQ和函数编程爱好者,你可以保存一个无用的循环,直接将你的匹配集合
转换成IEnumerable
:
如果需要安全转换,可以使用该方法,但如果正则表达式足够好,并且确保字符串的格式正确,则可以使用该方法将字符串转换为双精度:
var tmp3 = myregex.Matches(text_to_split);
foreach (Match match in tmp3)
{
double value = double.Parse(match.Value);
// TODO : do something with the matches value
}
如果你像我一样是LINQ和函数编程爱好者,你可以保存一个无用的循环,直接将你的匹配集合
转换成IEnumerable
:
如果您想要安全的转换,您可以使用该方法,但是如果您的正则表达式足够好,并且您已经确保字符串的格式正确,那么您应该可以。这正是我想要的。这正是我想要的。
var tmp3 = myregex.Matches(text_to_split);
var values = tmp3.Cast<Match>().Select(x => double.Parse(x.Value)).ToArray();