【问题标题】:Find all lines that contains given string查找包含给定字符串的所有行
【发布时间】:2013-05-13 17:50:17
【问题描述】:
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

【问题讨论】:

  • 什么错误? OutOfMemoryException? InvalidCastException?什么?
  • 别管什么错误,你试过调试吗?
  • 我建议添加文件示例以及您想从中获得什么。请注意,SO int a = Convert.ToInt16(s.Length); 中最搞笑的代码行之一。
  • 子字符串关闭,然后。你能给我们看一段数据列表的摘录吗?此外,File.ReadAllLines >>>> StreamReader + 手册。

标签: c# regex file search find


【解决方案1】:

这可能会有所帮助:

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

【讨论】:

  • Spec\\s 是正则表达式,不是一行的确切内容
【解决方案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));
    }
}

这可能行得通。

【讨论】:

  • 是的,它的工作。谢谢!但我需要使用正则表达式。
  • @user2395751 这是使用正则表达式的最愚蠢的任务。您不需要正则表达式来查找其中是否有一个字符串,然后是一个空格...
  • @user2395751 你可以把if(s.Contains("Spec")) {改成if(s.Contains("Spec ")) {,如果这是你要找的。​​span>
  • @newStackExchangeInstance 我需要在 Spec 和 Category 之间获取所有信息。这就是我使用正则表达式的原因。但我首先想让它发挥作用。
  • @user2395751 您当前的正则表达式不这样做...无论如何,只需将其包装在 Regex.IsMatch 中即可。
【解决方案3】:

通过查看您的示例文本文件,您开始延迟一个字符的子字符串。 额外的字符在那里,因为字符串是零索引的

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

【讨论】:

    【解决方案4】:

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

    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.
    

    希望即使在你解决了这个问题之后它也会对你有所帮助。

    【讨论】:

      猜你喜欢
      • 2022-09-28
      • 2015-12-27
      • 1970-01-01
      • 2018-01-26
      • 2015-12-31
      • 2014-10-20
      • 2011-01-28
      • 1970-01-01
      相关资源
      最近更新 更多