2014-09-02 2 views
0

여기에 약간의 푸시가 필요합니다. 나는 APP_ID = 다음 수에 문자열 배열을 얻을 필요가문자열 뒤에 나오는 모든 문자열 찾기

xyz buildinfo app_id="12345" asf 
sfsdf buildinfo app_id="12346" wefwef 
... 

같은 데이터가 포함 된 파일이 있습니다. 아래 코드는 나에게 모든 일치를 제공하고 나는 (Regex.Matches (text, searchPattern) .Count) 카운트를 얻을 수 있습니다. 하지만 배열에 실제 항목이 필요합니다.

string searchPattern = @"app_id=(\d+)"; 
       var z = Regex.Matches(text, searchPattern); 
+1

'새 정규식 (의 searchPattern) .Match (텍스트) .Captures' (또는'.Groups') – knittl

+0

Regex가 절대적으로 필요합니까? 아니면 문자열 메소드를 사용 하시겠습니까? – terrybozzio

답변

0

나는 당신이 APP_ID 부분이없는 항목 (숫자를) 원하는 말을하는지 생각 :

하면이 코드를 사용하여 그것을 인용. 당신은 패턴과 일치하는 Positive Lookbehind

string text = @"xyz buildinfo app_id=""12345"" asf sfsdf buildinfo app_id=""12346"" wefwef"; 
string searchPattern = @"(?<=app_id="")(\d+)"; 
var z = Regex.Matches(text, searchPattern) 
       .Cast<Match>() 
       .Select(m => m.Value) 
       .ToArray(); 

(?<=app_id="")를 사용하지만 캡처에 포함되지 할

+0

이것은 잘 작동합니다! 그러나 app_id = 12345와 같은 문자열을 선택합니다. 방금 번호가 필요해. RE를 어떻게 변경할 수 있습니까? 또는 Select – mhn

+0

에서 교체를 사용해야합니까? 문제가 발생합니다. 나는 아직도 나의 동일한 RE를 사용하고 있었다. – mhn

0

documentation을 살펴볼 수 있습니다.

string pattern = @"app_id=(\d+)"; 
    string input = "xyz buildinfo app_id="12345" asf sfsdf buildinfo app_id="12346" efwef"; 

    Match match = Regex.Match(input, pattern); 
    if (match.Success) { 
    Console.WriteLine("Matched text: {0}", match.Value); 
    for (int ctr = 1; ctr <= match.Groups.Count - 1; ctr++) { 
     Console.WriteLine(" Group {0}: {1}", ctr, match.Groups[ctr].Value); 
     int captureCtr = 0; 
     foreach (Capture capture in match.Groups[ctr].Captures) { 
      Console.WriteLine("  Capture {0}: {1}", 
          captureCtr, capture.Value); 
      captureCtr += 1;     
     } 
    } 
    }