【问题标题】:Find string in file using regular expression使用正则表达式在文件中查找字符串
【发布时间】:2013-05-18 00:36:46
【问题描述】:

对不起,我的英语不好,如果可以的话,请帮忙。

  private void button1_Click(object sender, EventArgs e)
            {
                 if (File.Exists(@"data.txt"))
                {
                    System.IO.StreamReader file = new System.IO.StreamReader(@"data.txt");

                     while (file.EndOfStream != true)
                      {
                        string s = file.ReadLine();
                        string s2 = file.ReadLine();
                        string s3 = file.ReadLine();
                        string s4 = file.ReadLine();

                        Match m = Regex.Match(s, "ID\\s");
                        Match m2 = Regex.Match(s2, "Spec\\s");
                        Match m3 = Regex.Match(s3, "Category\\s");
                        Match m4 = Regex.Match(s4, "Price\\s");

                        if (m.Success && m2.Success && m3.Success && m4.Success)
                        {

                             // some code 

                        }
                      }
                }
                if (!File.Exists(@"data.txt")) MessageBox.Show("The file is missing!");
            }

文本文件内容:

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

我只想获取ID 之后的文本,Spec 之后直到Category 的所有内容。 在这个例子中(上面)我需要:

560  

This ... bla bla 

    blah...
    blah...
    bla bla 
    bla


561

more blah blah...

    blah...
    blah...
    bla bla 
    bla

以此类推,直到到达文件末尾。

【问题讨论】:

    标签: c# regex search find


    【解决方案1】:

    我解析了一堆类似这样的文件。使用文本阅读器将数据读入 Ienumerable,如下所示。这不是工作代码,而是为您提供概念。它应该能让你朝着正确的方向前进。

     TextReader reader = new StreamReader(path);
     IEnumerable<string> data = this.ReadLines(reader);  
    
     foreach (var s in data){
         // make sure its not null doesn't start with an empty line or something.
         if (s != null && !string.IsNullOrEmpty(s) && !s.StartsWith("  ") && s.Length > 0){
            s = s.ToLower().Trim();
    
            // use regex to find some key in your case the "ID".
            // look into regex and word boundry find only lines with ID
            // double check the below regex below going off memory. \B is for boundry
            var regex = new Regex("\BID\B");
            var isMatch = regex.Match(s.ToLower());
            if(isMatch.Success){ 
               // split the spaces out of the line.
               var arr = s.split(' ');
               var id = arr[1]; // should be second obj in array.
    
            }
    
         }
     }
    

    这是我在实际项目中使用的一个解析文件,用于解析这种类型的文本文件。它使用 xml 文件进行模板化,以便该文件可用于各种文件。但是,它会让您了解什么是可能的,或者其他一些可以提供帮助的想法。 parse.cs

    【讨论】:

    • 谢谢伙计,但我是 C# 新手,不能自己做。
    • 以上内容对您来说实际上应该非常接近。只需将“路径”替换为相关文件的路径即可。 Stack 并不是真正旨在提供完整的解决方案。如果您需要雇用某人,您可以查看 ODesk 或类似的东西。真的,您应该再次尝试上述方法,但它应该相当接近。欢迎编辑/回帖,我会帮助你完成它。
    • 感谢您的建议,也许我应该去那里试试!
    • 很高兴为您提供帮助。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-06-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-07-26
    相关资源
    最近更新 更多