2013-03-07 27 views
-4

过滤值我希望我的计划,放弃所有这些线路中,“APPGUID”的值是“WX”或“空”使用正则表达式在C#

我怎样才能做到这一点使用正则表达式。在此之后,它应该返回“WX Edit Bug”的唯一值。我试过但看起来像我的代码中的一些错误。

我无法弄清楚它的正则表达式模式。请帮忙。

我的日志文件格式:

INFO [com.adobe.watson.vo.BugServices] WX Edit Bug: 3494430 Server: yukon.corp.adobe.com User:xinche appGUID: null INFO [com.adobe.watson.vo.BugServices] WX Edit Bug: 3494430 Server: yukon.corp.adobe.com User:xinche appGUID: null INFO [com.adobe.watson.vo.BugServices] WX Edit Bug: 3419432 Server: yukon.corp.adobe.com User:prerelease appGUID: fcdd2153-bbdf INFO [com.adobe.watson.vo.BugServices] WX Edit Bug: 3419432 Server: yukon.corp.adobe.com User:prerelease appGUID: fcdd2153-bbdf INFO [com.adobe.watson.vo.BugServices] WX Edit Bug: 3494430 Server: yukon.corp.adobe.com User:xinche appGUID: wx INFO [com.adobe.watson.vo.BugServices] WX Edit Bug: 3494430 Server: yukon.corp.adobe.com User:xinche appGUID: wx INFO [com.adobe.watson.vo.BugServices] WX Edit Bug: 3494430 Server: yukon.corp.adobe.com User:xinche appGUID: null INFO [com.adobe.watson.vo.BugServices] WX Edit Bug: 3494430 Server: yukon.corp.adobe.com User:xinche appGUID: null INFO [com.adobe.watson.vo.BugServices] WX Edit Bug: 3419432 Server: yukon.corp.adobe.com User:prerelease appGUID: fcdd2153-bbdf INFO [com.adobe.watson.vo.BugServices] WX Edit Bug: 3419432 Server: yukon.corp.adobe.com User:prerelease appGUID: fcdd2153-bbdf

我的代码是在这里:

IEnumerable<string> textLines 
          = Directory.GetFiles(@"C:\Users\karansha\Desktop\Dummy\", "*.*") 
          .Select(filePath => File.ReadLines(filePath)) 
          .SelectMany(line => line); 
      string x = string.Join(",", textLines); 
      Regex regex = new Regex(@"(.*)?(wx|null)\b"); 
      var newString = regex.Replace(x, String.Empty); 
      string discard = string.Join(",", newString); 
      List<string> users = new List<string>(); 
      Regex regex1 = new Regex(@"WX Edit Bug:\s*(?<value>.*?)\s+Server"); 
      MatchCollection matches1 = regex1.Matches(discard); 
      foreach (Match match in matches1) 
      { 
       var Editbug = match.Groups["value"].Value; 
       if (!users.Contains(Editbug)) users.Add(Editbug); 
      } 
      int WX_Edit = users.Count; 
+0

只是为了CLAR你能展示预期的产出吗? – 2013-03-07 15:22:05

+0

http://stackoverflow.com/a/15264675/470005 – 2013-03-07 15:24:05

回答

1

使用你提供的代码,并假设您的查询的第一部分工作,你可以尝试使用一个Where条款(未经测试)

IEnumerable<string> textLines 
    = Directory.GetFiles(@"C:\Users\karansha\Desktop\Dummy\", "*.*") 
       .Select(filePath => File.ReadLines(filePath)) 
       .SelectMany(line => line) 
       .Where(line => line.Contains("appGUID: null") || line.Contains("appGUID: wx")) 
       .ToList(); 
+0

bool不包含定义为列表 – 2013-03-07 15:31:01

+0

它给出了一个错误:布尔没有包含一个定义列表 – 2013-03-07 15:33:40

+0

嗯,我刚刚重新创建OP的文件,并跑上面的代码反对它。不幸的是,它将整个文件作为单行返回,但它*找到了该行并将其返回到'textLines'中,不会引发错误。 – 2013-03-07 15:41:34