2013-05-09 3 views
0

다음과 같이 로그 파일에서 다음 줄에서 특정 (bug.updateBug) 문자열을 가져 오기 위해 정규식을 만드는 방법 : WX 편집 버그 : 3,550,704 서버 : 서버 사용자 : testuser를 APPGUID : appguidvalue 경과 시간 : 624ms 방법 : bug.updateBug 날짜 : 수요일 5월 1일 태평양 서머 타임 09시 38분 1초 당신의 형태로 데이터를 보인다 2013로그 파일에서 특정 (bug.updateBug) 문자열을 가져 오는 정규식을 만드는 방법

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.IO; 
using System.Text.RegularExpressions; 

namespace ConsoleApplication59 
{ 
    class Program 
    { 
     static void Main(string[] args) 
     { 
      IEnumerable<string> textLines 
          = Directory.GetFiles(@"C:\testlocation\", "*.*") 
          .Select(filePath => File.ReadLines(filePath)) 
          .SelectMany(line => line); 

      List<string> users = new List<string>(); 
      Regex r = new Regex("^.*WX\\sADVSearch(.*)50\\]$"); 
      foreach (string Line in textLines) 
      { 
       if (r.IsMatch(Line)) 
       { 
        users.Add(Line); 
       } 
      } 
      string[] textLines1 = new List<string>(users).ToArray(); 
      int countlines = textLines1.Count(); 
      Console.WriteLine("WX_ADVSearch=" + countlines); 
      // keep screen from going away 
      // when run from VS.NET 
      Console.ReadLine(); 
     } 
    } 
} 

답변

0

키/값 쌍과 같은 것입니다. 이 정규식을 사용해보십시오 : 여기

(?<=method\:)([^ ]+) 

:

방법 값을 찾을 수있는 키입니다. 그리고 다음 키를 제외한 모든 것이 그 값으로 간주됩니다.

희망이 있습니다.

편집

 static void Main(string[] args) 
     { 
      IEnumerable<string> textLines = Directory.GetFiles(@"C:\testlocation\", "*.*") 
          .Select(filePath => File.ReadLines(filePath)) 
          .SelectMany(line => line); 

      //List<string> users = new List<string>(); 
      //Regex r = new Regex("^.*WX\\sADVSearch(.*)50\\]$"); 
      string text = String.Join("",textLines.ToArray()); 

      MatchCollection mcol = Regex.Matches(text,"(?<=method\:)([^ ]+)"); 
      //foreach (string Line in textLines) 
      //{ 
      // if (r.IsMatch(Line)) 
      // { 
      //  users.Add(Line); 
      // } 
      //} 
      //string[] textLines1 = new List<string>(users).ToArray(); 
      int countlines = mcol.Count; //textLines1.Count(); 
      Console.WriteLine("WX_ADVSearch=" + countlines); 
      // keep screen from going away 
      // when run from VS.NET 
      Console.ReadLine(); 
     } 
+0

감사합니다. 글쎄 실제로 나는 User : github 및 method : bug.updatebug가있는 로그 파일에있는 모든 줄에서 발생 수를 추출하고 싶습니다. 위와 같은 줄의 예는 다음과 같습니다. WX Edit Bug : 3550704 Server : servername User : github appGUID : appguidvalue 경과 시간 : 624ms 메서드 : bug.updateBug 날짜 : Wed May 01 09:38:01 PDT 2013 – user2364821

+0

'MatchCollection' 클래스를'Regex.Matches() '와 함께 사용하면 Regex 엔진에서 찾은 일치 항목. – NeverHopeless

+0

흠. 실제로 나는 정규식에 새로운 있습니다. 앞선 코멘트에서 지정한대로 정규 표현식을 공유 할 수 있습니까? 감사합니다 – user2364821

관련 문제