Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/335.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# 将MatchCollection转换为Double数组_C#_Regex - Fatal编程技术网

C# 将MatchCollection转换为Double数组

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

我有以下生成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 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();