2013-03-07 63 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