2012-07-10 8 views
0

한 줄씩 텍스트 파일을 구문 분석하고 있는데, 여러 개의 일치 항목 컬렉션을 만들고 각 줄마다 여러 항목을 처리하려면 어떻게해야합니까?한 줄에 여러 개의 일치 항목이 있습니다.

MessageBox.Show(line1.Count + " " + line2.Count + " " + line3.Count); 

내가 0 0 5000 왜 내 마지막 2 arraylists가 비어 나타납니다 파일을 처리하고 ArrayList의 크기를 계산하려고하면

while ((line = reader.ReadLine()) != null) { 

string match1 = @"\s+([^)]*):entry:\s+cust\(([^)]*)\)\s+custno\(([^)]*)\)\s+id\(([^)]*)\)\s+name\(([^)]*)\)"; 
string match2 = @"group\(([^)]*)\)\s+spec\(([^)]*)\)\s+goodtill([^)]*)$"; 
string match3 = @"returns\(([^)]*)\)"; 

MatchCollection matches = Regex.Matches(line, match1); 
MatchCollection matches2 = Regex.Matches(line, match2); 
MatchCollection matches3 = Regex.Matches(line, match3); 




foreach (Match matchr1 in matches) 
{ 

    line1.Add("Date:" + matchr1.Groups[1].Value + ", Customer:" 
        + matchr1.Groups[2].Value + ", CustID:" + matchr1.Groups[3].Value + 
        ", ID:" + matchr1.Groups[4].Value + ", Name:" + matchr1.Groups[5].Value); 
} 

foreach (Match matchr2 in matches2) 
{ 

    line2.Add("Group:" + matchr2.Groups[1].Value + ", Spec:" + matchr2.Groups[2].Value + ", Good Till:" + matchr2.Groups[3].Value); 
} 

foreach (Match matchr3 in matches3) 
{ 

    line3.Add("Returns: " + matchr3.Groups[1].Value); 
} 

} 

:

현재 나는이 노력하고 있어요? 많은 일치가 있어야합니다, 정규식이 올바른지 확인합니다.

샘플 데이터 :

LUCIE:496 27AUG120755:entry: cust(GUIR) custno(j010705) id(293746) name(mike) 
LUCIE:496 27AUG120755:  group(0000) spec(03) stripdn(N) pre228(N)  goodtill 01/MAR/08 
LUCIE:496 27AUG120755:getprotcode given (m000029374603MAR08), returns (TUUjFDEO) 
+2

두 번째 및 세 번째 정규 표현식이 전혀 일치하지 않으므로? 우리는 당신의 선이 어떻게 생겼는지 모른 채 명확한 답을 줄 수는 없습니다. 독자가 예상 한 결과를 얻고 있는지 확인하기 위해 독자와 소장 도서의 내용을 살펴 보았습니까? 왜 ArrayLists를 사용하고 있습니까? –

+0

Regex.Matches를 처음 호출 한 후에도 회선에 내용이 계속 있는지 확인하십시오. API에서 알아 차 렸던 또 다른 사실은 "컬렉션에는 성공적인 성냥 만 포함되고 첫 번째 실패한 성냥에서 끝납니다."입니다. 메소드가 일찍 종료 될 수 있습니까? 테스트 할 파일을 제공 할 수 있습니까? .... ref : http://msdn.microsoft.com/en-us/library/b9712a7w.aspx – Tom

+0

@Jeff는 regexbuddy에서 관련 줄과 함께 정규식을 확인했습니다. 파일에는 기밀 고객 데이터가 포함되어 있으므로 샘플 데이터를 게시합니다. –

답변

0

사용자가 데이터에 문자가 될 것 경우, 2 층과 3 정규식의에서 누락 된 구성 요소를 필요로한다. 또는 변수가 있으면 그냥 채우기를 추가하십시오. .*?

group 
\(
    ([^)]*) 
\) 
\s+ spec 
\(
    ([^)]*) 
\) 
    # missing : 
    #  \s+ stripdn 
    #  \(
    #  ([^)]*) 
    #  \) 
    #  \s+ pre228 
    #  \(
    #  ([^)]*) 
    #  \) 

\s+ goodtill 
([^)]*) 
$ 


returns 
    # missing a \s* here 
\(
    ([^)]*) 
\) 


LUCIE:496 27AUG120755:entry: cust(GUIR) custno(j010705) id(293746) name(mike) 
LUCIE:496 27AUG120755:  group(0000) spec(03) stripdn(N) pre228(N)  goodtill 01/MAR/08 
LUCIE:496 27AUG120755:getprotcode given (m000029374603MAR08), returns (TUUjFDEO) 
관련 문제