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

  • Home
  • SEARCH
  • 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 253133
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T21:44:10+00:00 2026-05-11T21:44:10+00:00

In a program I’m reading in some data files, part of which are formatted

  • 0

In a program I’m reading in some data files, part of which are formatted as a series of records each in square brackets. Each record contains a section title and a series of key/value pairs.

I originally wrote code to loop through and extract the values, but decided it could be done more elegantly using regular expressions. Below is my resulting code (I just hacked it out for now in a console app – so know the variable names aren’t that great, etc.

Can you suggest improvements? I feel it shouldn’t be necessary to do two matches and a substring, but can’t figure out how to do it all in one big step:

string input = "[section1 key1=value1 key2=value2][section2 key1=value1 key2=value2 key3=value3][section3 key1=value1]";

MatchCollection matches=Regex.Matches(input, @"\[[^\]]*\]");
foreach (Match match in matches)
{
    string subinput = match.Value;

    int firstSpace = subinput.IndexOf(' ');
    string section = subinput.Substring(1, firstSpace-1);
    Console.WriteLine(section);

    MatchCollection newMatches = Regex.Matches(subinput.Substring(firstSpace + 1), @"\s*(\w+)\s*=\s*(\w+)\s*");
    foreach (Match newMatch in newMatches)
    {
        Console.WriteLine("{0}={1}", newMatch.Groups[1].Value, newMatch.Groups[2].Value);
    }
}
  • 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-05-11T21:44:10+00:00Added an answer on May 11, 2026 at 9:44 pm

    I prefer named captures, nice formatting, and clarity:

    string input = "[section1 key1=value1 key2=value2][section2 key1=value1 key2=value2 key3=value3][section3 key1=value1]";
    MatchCollection matches = Regex.Matches(input, @"\[
                                                        (?<sectionName>\S+)
                                                          (\s+                                                            
                                                             (?<key>[^=]+)
                                                              =
                                                             (?<value>[^ \] ]+)                                                    
                                                          )+
                                                      ]", RegexOptions.IgnorePatternWhitespace);
    
    foreach(Match currentMatch in matches)
    {
        Console.WriteLine("Section: {0}", currentMatch.Groups["sectionName"].Value);
        CaptureCollection keys = currentMatch.Groups["key"].Captures;
        CaptureCollection values = currentMatch.Groups["value"].Captures;
    
        for(int i = 0; i < keys.Count; i++)
        {
            Console.WriteLine("{0}={1}", keys[i].Value, values[i].Value);           
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Most program languages have some kind of exception handling; some languages have return codes,
A program receives a list of Messages (base type). Each message in the list
My program has to read files that use various encodings. They may be ANSI,
My program is crash intermittently when it tries to copy a character array which
Program followed by output. Someone please explain to me why 10,000,000 milliseconds from Jan
My program generates relatively simple PDF documents on request, but I'm having trouble with
My Program overrides public void paint(Graphics g, int x, int y); in order to
What program can I use to decompile a class file? Will I actually get
Strange program hang, what does this mean in debug? After attaching windbg I found
The program that I am currently assigned to has a requirement that I copy

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.