查找包含给定字符串的所有行
System.IO.StreamReader file = new System.IO.StreamReader(@"data.txt"); List Spec= new List(); while (file.EndOfStream != true) { string s = file.ReadLine(); Match m = Regex.Match(s, "Spec\\s"); if (m.Success) { int a = Convert.ToInt16(s.Length); a = a - 5; string part = s.Substring(5, a); Spec.Add(part); } }
我正在尝试获取包含单词“Spec”的所有行,然后是空格字符,但是当我运行此程序时出现错误。
例外的细节如下:
An unhandled exception of type 'System.ArgumentOutOfRangeException' occurred in mscorlib.dll
任何人都可以协助我找出原因吗?
文本文件:
ID 560 Spec This ... bla bla blah... blah... bla bla bla Category Other Price $259.95 ID 561 Spec more blah blah... blah... blah... bla bla bla Category Other Price $229.95
System.IO.StreamReader file = new System.IO.StreamReader("data.txt"); List Spec = new List (); while (!file.EndOfStream) { if(file.ReadLine().Contains("Spec")) { Spec.Add(s.Substring(5, s.Length - 5)); } }
那可能有用。
这可能有所帮助:
var result = System.IO.File .ReadAllLines(@"data.txt") .Where(i => i.Contains("Spec")) .ToList();
从查看示例文本文件开始,您将开始使用一个字符串。 当字符串被零索引时,额外的字符就在那里
string part = s.Substring(4, s.Length - 4);
我的测试代码
string s = "Spec This ... bla bla"; Console.WriteLine(s.Substring(4,s.Length-4)); Console.ReadLine(); output:= This ... bla bla
我知道这个线程已经解决了,但是如果你想使用正则表达式,你需要在现有代码中进行一些调整:
System.IO.StreamReader file = new System.IO.StreamReader(@"data.txt"); List Spec= new List (); while (file.EndOfStream != true) { string s = file.ReadLine(); Match m = Regex.Match(s, "(?<=Spec\s)(.)+"); if (m.Success) { Spec.Add(m.ToString()); } s = String.Empty; // do not forget to free the space you occupied. }
这里:
(?<=Spec\s) : This part looks for the text "Spec " in line. Also known as positive look behind. (.)+ : If first part satisfies take the whole line as a matched string. "." matches every thing except newline.
希望它能帮助你解决这个问题。