Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7827679
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T09:59:27+00:00 2026-06-02T09:59:27+00:00

TL;DR I am parsing a report using C# regex, MultiLine enabled, processing the whole

  • 0

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
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T09:59:28+00:00Added an answer on June 2, 2026 at 9:59 am

    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:

    (?:
       (?: Section ...  (?<sec_7> 7)
       )
     | (?: Section ...  (?<sec_C> C)
       )?
     | (?: Section ...  (?<sec_Z> Z)
       )
    )
    (?: Section ...  (?!\k<sec_7>) (?<sec_7>  7) )?
    (?: Section ...  (?!\k<sec_C>) (?<sec_C>  C) )?
    (?: Section ...  (?!\k<sec_Z>) (?<sec_Z>  Z) )?
    

    If it can be factored, then this way:

    (?: Section ...  (?<sec_a>(?:7|C|Z) )
    (?: Section ...  (?<sec_b>(?!\k<sec_a>)(?:7|C|Z)  )?
    (?: Section ...  (?<sec_c>(?!\k<sec_a>|\k<sec_b>)(?:7|C|Z)  )?
    #
    # Then after match check <sec_a/b/c> for its value
    

    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:

    # base 10 statistic sections
        (?: ..)
      |
        (?: ..)
      |
        (?: ..)
    

    Where each match in ‘base 10’ section match has to be checked in a while loop as

    Match m = Regex.Match(input, regex, RegexOptions.IgnorePatternWhitespace);
    while (m.Success)
    {
       if (m.Groups["base10_Section7"].Success)  {    }
       else
       if (m.Groups["base10_SectionZ"].Success)  {    }
       else
       if (m.Groups["base10_SectionC"].Success)  {    }
       m = m.NextMatch();
    }
    

    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.

    string input = @"
        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 P           16.1 __ base 2
        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 r           49.2 __ Base 2
    ";
    
    string regex = @"
       # base 10 statistic sections
           (?:
             [\s-[\n\r]]*(?i:Section\s(?<base10_Section>Z|7|C)\s+(?<Base10>\d+\.\d|\d+)\s__\sbase)\s10
             (?:\r?\n)+
             (?:\s+(?<Instance>\d+\.\d|\d+)\s__\s(?<Time>\d\d/\d\d\s\d\d:\d\d:\d\d)(?:\r?\n)+)+
           )
         |  # Or, base 2 statistic sections
           (?:
             [\s-[\n\r]]*(?i:Section\s(?<base2_Section>R|P)\s+(?<Base2>\d+\.\d|\d+)\s__\sbase)\s2
             (?:\r?\n)+
           )
       # |  Or, something else
    
    ";
    
    Match m = Regex.Match(input, regex, RegexOptions.IgnorePatternWhitespace);
    int matchCount = 0;
    while (m.Success)
    {
        Console.WriteLine("\nMatch " + (++matchCount) + "\n------------------");
        // Check base 10
        if (m.Groups["base10_Section"].Success)
        {
            Console.WriteLine("Section (base10)  '" + m.Groups["base10_Section"] + "'  =  '" + m.Groups["Base10"] + "'\n");
    
            int count = m.Groups["Instance"].Captures.Count;
            // Instance
            for (int j = 0; j < count; j++)
                System.Console.WriteLine("    Instance (" + j + ") =  '" + m.Groups["Instance"].Captures[j] + "' ");
            // Time
            for (int j = 0; j < count; j++)
                System.Console.WriteLine("    Time(" + j + ") =  '" + m.Groups["Time"].Captures[j] + "' ");
            // Combined ..
            for (int j = 0; j < count; j++)
                System.Console.WriteLine("    Instance,Time  (" + j + ") =  '" +
                                              m.Groups["Instance"].Captures[j] + "' __ '" +
                                              m.Groups["Time"].Captures[j] + "' ");
        }
        else
        // Check base 2
        if (m.Groups["base2_Section"].Success)
            Console.WriteLine("Section (base2)  '" + m.Groups["base2_Section"] + "'  =  '" + m.Groups["Base2"] + "'\n");
    
        m = m.NextMatch();
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Im parsing the source of a website and Im using this regex: /page\.php\?id\=([0-9]*)\\>(.*)\<\/a\>\<\/span\>/.match(self.agent.page.content) self.agent.page.content
I'm parsing a binary file format. It encodes an integer using four bytes in
i'm trying to parsing data to the php file and retrive data using jsons.It
When parsing an xml file in android, I'm doing like this: try { InputStream
I am parsing some HTML source. Is there a regex script to find out
I am developing report containing sub report using iReport and i am passing two
Scenario: Sending XML, generated using php, via cURL to an external server for parsing.
I am developing a report using iReport where I want to add a condition
I'm trying to import a moderately complex report into my java application. The report
I am using the ReportExecutionService web service to run an SSRS report that has

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.