RegEx文本中的多个匹配项

时间:2010-12-08 18:19:17

标签: c# regex

4 个答案:

答案 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
------------------------

答案 1 :(得分:1)

虽然每匹配使用额外的正则表达式,但是acoolaum的答案是正确的。我更改了他的代码,因此它只使用一个正则表达式。这是代码:

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();
}

请注意我使用代码更改了正则表达式,以便从Regex中提取匹配项(我使用Group.Captures属性来提取组“Error”的多个匹配项。)

输出:

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
------------------------

答案 2 :(得分:0)

答案 3 :(得分:0)