2010-12-08 22 views
1

問題のレコードに複数のエラーがある場合を除いて、電子メールとその作業のほとんどを解析しようとしています。RegEx複数のテキストの一致

はここで私が使用している正規表現のテキストここ

Record #1 with LeadRecordID 4 and MTN of (813) 555-1234 has 4 errors: 
    Shipping Street Address cannot be blank 
    Shipping City cannot be blank 
    Shipping Zipcode cannot be blank 
    Errors exist in secondary records #2, #3, #4, record not processed. 
Record #2 with LeadRecordID 5 and MTN of (813) 555-4321 has 1 errors: 
    Shipping Street Address cannot be blank 

の一部されます:

Record #(?<RecordNumber>\d*) with LeadRecordID (?<LeadRecordId>\d*) and MTN of .* has (?<NumberOfErrors>\d*) errors:(?:\r\n|)* (?<Error1>.*) 

編集:私はこれを行う場合はが、私はエラーで、2試合を得る グループは1グループにつき1つの一致のみを示し、すべてのエラーラインを表示する必要があります。LeadRecordID(?\ d *)のレコード番号(?\ d *)と。*のMTNには(?:¥d *)というエラーがあります(?:\ r \ n) n)

編集2: これは私にサブグループを与えてくれたようです。

Record #(?<RecordNumber>\d*) with LeadRecordID (?<LeadRecordId>\d*) and MTN of [^\r\n]* has (?<NumberOfErrors>\d*) errors:(?:\r\n|)*(?<Errors>(?:(?<Error>\s{3}[^\r\n]+)(?:\r\n)*)+) 
enter code here 
+3

あなたの正規表現からする出力は何ですか?あなたの質問からは分かりません。 – mikel

+0

各レコードは一致している必要があります。エラーは一致の配列として出力されます。 –

答えて

1

このパターンとRegex.Matchesを使用してみてください:

@"Record #(?<RecordNumber>\d*) with LeadRecordID (?<LeadRecordId>\d*) and MTN of [^\r\n]* has (?<NumberOfErrors>\d*) errors:(?:\r\n|)*(?<Errors>(?:\s{3}[^\r\n]+(?:\r\n)*)+)" 

テストコード:

static void Main(string[] args) 
     { 
      string pattern = 
@"Record #(?<RecordNumber>\d*) with LeadRecordID (?<LeadRecordId>\d*) and MTN of [^\r\n]* has (?<NumberOfErrors>\d*) errors:(?:\r\n|)*(?<Errors>(?:\s{3}[^\r\n]+(?:\r\n)*)+)"; 

      string message = @"Record #1 with LeadRecordID 4 and MTN of (813) 555-1234 has 4 errors: 
    Shipping Street Address cannot be blank 
    Shipping City cannot be blank 
    Shipping Zipcode cannot be blank 
    Errors exist in secondary records #2, #3, #4, record not processed. 
Record #2 with LeadRecordID 5 and MTN of (813) 555-4321 has 1 errors: 
    Shipping Street Address cannot be blank"; 

      MatchCollection mc = Regex.Matches(message, pattern); 

      foreach (Match m in mc) 
      { 
       Console.WriteLine("RecordNumber = \"{0}\"", m.Groups["RecordNumber"].Value); 
       Console.WriteLine("LeadRecordId = \"{0}\"", m.Groups["LeadRecordId"].Value); 
       Console.WriteLine("NumberOfErrors = \"{0}\"", m.Groups["NumberOfErrors"].Value); 
       Console.WriteLine("Errors = \"{0}\"", m.Groups["Errors"].Value); 

       MatchCollection errors = Regex.Matches(m.Groups["Errors"].Value, @"\s{3}(?<error>[^\r\n]+)(?:\r\n)*"); 
       foreach(Match g1 in errors) 
       { 
        Console.WriteLine(g1.Groups["error"].Value); 
       } 
       Console.WriteLine("------------------------"); 
      } 
      Console.ReadLine(); 
     } 

結果:それは追加の定期を使用していますけれども

RecordNumber = "1" 
LeadRecordId = "4" 
NumberOfErrors = "4" 
Errors = " Shipping Street Address cannot be blank 
    Shipping City cannot be blank 
    Shipping Zipcode cannot be blank 
    Errors exist in secondary records #2, #3, #4, record not processed. 
" 
Shipping Street Address cannot be blank 
Shipping City cannot be blank 
Shipping Zipcode cannot be blank 
Errors exist in secondary records #2, #3, #4, record not processed. 
------------------------ 
RecordNumber = "2" 
LeadRecordId = "5" 
NumberOfErrors = "1" 
Errors = " Shipping Street Address cannot be blank" 
Shipping Street Address cannot be blank 
------------------------ 
+0

私はテストのためにExpressoを使用しており、そのパターンは動作していないようです –

+0

@Jason Carter、どのマッチングオプションを使用しますか? – acoolaum

+0

@ Jason Carter、私は自分のソリューションにRegExpの変更点を追加し、完全なエラーテキストをエラー行に分割しました。そして、私はあなたがテストのためにC#を使うべきだと思います。 – acoolaum

0

私はあなたのコードを見ずに言うことはできませんが、マッチングのためのオプションとして設定します何Regex.Match

0

とは対照的に、あなたはおそらく Regex.Matchesを必要としています。単一行または複数行?

正規表現を変更して複数行オプションを使用する必要があると思います。

+0

"$"と "^"を使用しなかったのでパターンを変更する必要があると思います。私の答えを見てください。 – acoolaum

+0

私はあなたが正しいと思います。 –

+0

複数行に設定されています。 –

1

acoolaumの答えが正しいですかマッチごとの表現。コードを変更して正規表現を1つだけ使用しました。ここでは、コードがあります:

static void Main(string[] args) 
{ 
    string pattern = 
@"Record #(?<RecordNumber>\d*) with LeadRecordID (?<LeadRecordId>\d*) and MTN of [^\r\n]* has (?<NumberOfErrors>\d*) errors:\r\n(?:\s{3}(?<Error>[^\r\n]+)(?:\r\n)*)+"; 

      string message = 
@"Record #1 with LeadRecordID 4 and MTN of (813) 555-1234 has 4 errors: 
    Shipping Street Address cannot be blank 
    Shipping City cannot be blank 
    Shipping Zipcode cannot be blank 
    Errors exist in secondary records #2, #3, #4, record not processed. 
Record #2 with LeadRecordID 5 and MTN of (813) 555-4321 has 1 errors: 
    Shipping Street Address cannot be blank"; 

    MatchCollection mc = Regex.Matches(message, pattern); 

    foreach (Match m in mc) 
    { 
     Console.WriteLine("RecordNumber = \"{0}\"", m.Groups["RecordNumber"].Value); 
     Console.WriteLine("LeadRecordId = \"{0}\"", m.Groups["LeadRecordId"].Value); 
     Console.WriteLine("NumberOfErrors = \"{0}\"", m.Groups["NumberOfErrors"].Value); 
     Console.WriteLine("Errors:"); 

     foreach (Capture capture in m.Groups["Error"].Captures) 
     { 
      Console.WriteLine("\t{0}", capture.Value); 
     } 
     Console.WriteLine("------------------------"); 
    } 
    Console.ReadLine(); 
} 

私は正規表現(私はグループの「エラー」の複数の一致を抽出するためにGroup.Capturesプロパティを使用)から一致するものを抽出するためのコードで正規表現自体を変更注意してください。

出力:

RecordNumber = "1" 
LeadRecordId = "4" 
NumberOfErrors = "4" 
Errors: 
     Shipping Street Address cannot be blank 
     Shipping City cannot be blank 
     Shipping Zipcode cannot be blank 
     Errors exist in secondary records #2, #3, #4, record not processed. 
------------------------ 
RecordNumber = "2" 
LeadRecordId = "5" 
NumberOfErrors = "1" 
Errors: 
     Shipping Street Address cannot be blank 
------------------------ 
+0

が追加されました。 :) – acoolaum