0
我在NET4.5此C#代碼讀取文本文件,該文件是這樣的:Regex.Matches錯誤無法從字符轉換成字符串
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:最好的OV對於「System.Text.RegularExpressions.Regex.Matches(字符串,字符串)」 erloaded方法匹配具有一些無效參數
錯誤CS1503:參數1:不能從「炭」轉換爲「字符串」
您可以使用'string.Split'而不是正則表達式。 – juharr
但數字之間有製表符和空格,我不知道分割是否與可變數量的製表符或數字之間的空格一起工作。 –
Jonesopolis的答案或多或少都是你想要的。 – bvoyelr