TL;DR
I am parsing a report using C# regex, MultiLine enabled, processing the whole file using a single (complex) regex pattern with named groups. (And CaptureCollection.)
Sections of my report appear out of order or are missing in a way I cannot predict.
How do I match them regardless of the order they appear in?
Preface
I am parsing a report using regular expressions in C# (.Net 3.5) using System.Text.RegularExpressions. One section of the report looks like this:
Section Z 0 __ base 10
2 __ 19/04 20:06:39
2 __ 19/04 20:15:49
1.8 __ 19/04 20:09:35
1.6 __ 19/04 20:07:01
1.6 __ 19/04 20:08:29
Section 7 0.8 __ base 10
8 __ 18/04 21:03:01
7.3 __ 18/04 21:02:17
3.7 __ 19/04 08:41:09
3.4 __ 19/04 00:13:08
3.3 __ 18/04 21:02:50
Section C 0 __ base 10
19.7 __ 19/04 10:25:06
11.1 __ 19/04 10:15:01
8.8 __ 19/04 10:14:50
7.2 __ 19/04 19:51:37
6.1 __ 19/04 14:19:47
My regex matches the text file as a whole, using the options (?mx) (MultiLine, IgnorePatternWhitespace). Because the statistic section has sub-statistics for each, I have resorted to manually crafting each section’s (optional ?) non-capturing group ((?:match_this_text)) and putting them in the pattern in the order I thought they were occurring, such as the following:
(?mx) #Turn on options multiline, ignore whitespace.
(?: # base 10 statistic sections
(?:
[\s-[\n\r]]*(?i:Section\sZ)\s+(?<base10_SectionZ>\d+\.\d|\d+)\s__\sbase\s10
(?:\r?\n)+
(?:\s+(?<base10_SectionZ_instance>\d+\.\d|\d+)\s__\s(?<base10_SectionZ_instance_time>\d\d/\d\d\s\d\d:\d\d:\d\d)(?:\r?\n)+)+
)?
(?:
[\s-[\n\r]]*(?i:Section\s7)\s+(?<base10_Section7>\d+\.\d|\d+)\s__\sbase\s10
(?:\r?\n)+
(?:\s+(?<base10_Section7_instance>\d+\.\d|\d+)\s__\s(?<base10_Section7_instance_time>\d\d/\d\d\s\d\d:\d\d:\d\d)(?:\r?\n)+)+
)?
(?:
[\s-[\n\r]]*(?i:Section\sC)\s+(?<base10_SectionC>\d+\.\d|\d+)\s__\sbase\s10
(?:\r?\n)+
(?:\s+(?<base10_SectionC_instance>\d+\.\d|\d+)\s__\s(?<base10_SectionC_instance_time>\d\d/\d\d\s\d\d:\d\d:\d\d)(?:\r?\n)+)+
)?
)
The first line of each section’s non-capturing group matches the ‘section header’, the second line matches the newline between the header and the statistic instances, and the third matches the individual statistic instances (repeating, n number of instances).
The Problem
The program that generates this report, depending on which version is running, outputs each section (e.g. Section Z, Section 7, Section C) in different orders, and certain sections are missing in certain circumstances. When I ran it against a second test file, it failed because the sections were out of order.
Thus, section C could occur before Section Z, but the regex pattern is expecting Z to occur before C.
Basically, I want the same regex to match and extract (using the named groups above) the same data regardless of the order the sections appear in, such that it matches both the test data above, and this test data:
Section 7 0.8 __ base 10
8 __ 18/04 21:03:01
7.3 __ 18/04 21:02:17
3.7 __ 19/04 08:41:09
3.4 __ 19/04 00:13:08
3.3 __ 18/04 21:02:50
Section C 0 __ base 10
19.7 __ 19/04 10:25:06
11.1 __ 19/04 10:15:01
8.8 __ 19/04 10:14:50
7.2 __ 19/04 19:51:37
6.1 __ 19/04 14:19:47
Section Z 0 __ base 10
2 __ 19/04 20:06:39
2 __ 19/04 20:15:49
1.8 __ 19/04 20:09:35
1.6 __ 19/04 20:07:01
1.6 __ 19/04 20:08:29
You shouldn’t give the regex engine the option to match nothing.
It will roam around finding a lot of ‘nothing’ before it finds an optional something.
edit
If you just want a block match (any order, but sequential) something like this will work.
Your way now, with modifications:
If it can be factored, then this way:
If you don’t care about a block match:
Your case revolves around just an OR condition.
So, it could be as easy as this:
Where each match in ‘base 10’ section match has to be checked in a while loop as
Even this can be reduced. For instance 7,Z,C can be combined in a single chunk.
This will leave the OR (|) for other distinct items to match, like say, ‘base 2’,
or any other form. One form will match. It has to be checked anyway.