Regex.Matches错误无法从char转换为字符串

时间:2015-10-06 19:53:22

标签: c# .net regex string

我在.NET4.5中有这个c#代码来读取一个如下所示的文本文件:

1   3          10.1144881901           48.8578515599          340.2980957031           -3.9997586182           -2.0398821492          -56.6352938643
2   1          10.1137751593           48.8575005060          401.4981384277          -11.7762306910            3.4075851669          -92.5498187137

我想更改第5行数

这是代码

while ((line = file.ReadLine()) != null)
{
     String pattern = @"(\d+)\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)";
     foreach (var expression in line)
         foreach (Match m in Regex.Matches(expression, pattern))
         {                        
            double value = Double.Parse(m.Groups[5].Value);
         }            
}

我得到了这些编译错误:

错误CS1502:最佳重载方法匹配' System.Text.RegularExpressions.Regex.Matches(string,string)'有一些无效的论点

错误CS1503:参数1:无法转换为' char'到'字符串'

1 个答案:

答案 0 :(得分:3)

摆脱你的外在的foreach:

while ((line = file.ReadLine()) != null)
{
     String pattern = @"(\d+)\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)";

     foreach (Match m in Regex.Matches(line, pattern))
     {                        
        double value = Double.Parse(m.Groups[5].Value);
     }            
}

你正在迭代你的字符串,这是一个字符数组。