查找包含给定字符串的所有行

时间:2013-05-18 19:14:36

标签: c# regex file search find

System.IO.StreamReader file = new System.IO.StreamReader(@"data.txt");
List<String> Spec= new List<String>();
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

4 个答案:

答案 0 :(得分:3)

这可能会有所帮助:

var result = System.IO.File
    .ReadAllLines(@"data.txt")
    .Where(i => i.Contains("Spec"))
    .ToList();

答案 1 :(得分:2)

System.IO.StreamReader file = new System.IO.StreamReader("data.txt");
List<string> Spec = new List<string>();
while (!file.EndOfStream)
{
    if(file.ReadLine().Contains("Spec")) 
    {
        Spec.Add(s.Substring(5, s.Length - 5));
    }
}

这可能有效。

答案 2 :(得分:1)

从查看示例文本文件开始,您将开始使用一个字符串。 当字符串为零索引

时,会有额外的字符
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

答案 3 :(得分:1)

我知道这个线程已经解决了,但如果你想使用正则表达式,你需要在现有代码中进行一些调整:

System.IO.StreamReader file = new System.IO.StreamReader(@"data.txt");
List<String> Spec= new List<String>();
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.

希望即使您解决了这个问题,它也会对您有所帮助。