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 4245262
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T03:49:02+00:00 2026-05-21T03:49:02+00:00

I hope you guys can help me out. I’m using C# .Net 4.0 I

  • 0

I hope you guys can help me out.
I’m using C# .Net 4.0

I want validate file structure like

 
const string dataFileScr = @"
Start 0
{
    Next = 1
    Author = rk
    Date = 2011-03-10
/*  Description = simple */
}

PZ 11
{
IA_return()
}

GDC 7
{
    Message = 6
    Message = 7
        Message = 8
        Message = 8
    RepeatCount = 2
    ErrorMessage = 10
    ErrorMessage = 11
    onKey[5] = 6
    onKey[6] = 4
    onKey[9] = 11
}
";

So far I managed to build this regex pattern

 
const string patternFileScr = @"
^                           
((?:\[|\s)*                  

     (?<Section>[^\]\r\n]*)     
 (?:\])*                     
 (?:[\r\n]{0,}|\Z))         
(
    (?:\{)                  ### !! improve for .ini file, dont take { 
    (?:[\r\n]{0,}|\Z)           
        (                          # Begin capture groups (Key Value Pairs)
        (?!\}|\[)                    # Stop capture groups if a } is found; new section  

          (?:\s)*                     # Line with space
          (?<Key>[^=]*?)            # Any text before the =, matched few as possible
          (?:[\s]*=[\s]*)                     # Get the = now
          (?<Value>[^\r\n]*)        # Get everything that is not an Line Changes


         (?:[\r\n]{0,})
         )*                        # End Capture groups
    (?:[\r\n]{0,})
    (?:\})?
    (?:[\r\n\s]{0,}|\Z)
)*

                ";

and c#


  Dictionary <string, Dictionary<string, string>> DictDataFileScr
            = (from Match m in Regex.Matches(dataFileScr,
                                            patternFileScr,
                                            RegexOptions.IgnorePatternWhitespace | RegexOptions.Multiline)
               select new
               {
                   Section = m.Groups["Section"].Value,

                   kvps = (from cpKey in m.Groups["Key"].Captures.Cast().Select((a, i) => new { a.Value, i })
                           join cpValue in m.Groups["Value"].Captures.Cast().Select((b, i) => new { b.Value, i }) on cpKey.i equals cpValue.i
                           select new KeyValuePair(cpKey.Value, cpValue.Value)).OrderBy(_ => _.Key)
                           .ToDictionary(kvp => kvp.Key, kvp => kvp.Value)

               }).ToDictionary(itm => itm.Section, itm => itm.kvps);

It works for

 
const string dataFileScr = @"
Start 0
{
    Next = 1
    Author = rk
    Date = 2011-03-10
/*  Description = simple */
}

GDC 7
{
    Message = 6
    RepeatCount = 2
    ErrorMessage = 10
    onKey[5] = 6
    onKey[6] = 4
    onKey[9] = 11
}
";

in other words

 
Section1
{
key1=value1
key2=value2
}

Section2
{
key1=value1
key2=value2
}

, but

  • 1. not for multiple keyname, i want group by key and output
  • 
    DictDataFileScr["GDC 7"]["Message"] = "6|7|8|8"
    DictDataFileScr["GDC 7"]["ErrorMessage"] = "10|11"
    
  • 2. not work for .ini file like
  • 
    ....
    [Section1]
    key1 = value1
    key2 = value2
    
    [Section2]
    key1 = value1
    key2 = value2
    ...
    
  • 3. dont see next section after
  • 
    ....
    PZ 11
    {
    IA_return()
    }
    .....
    
    • 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-21T03:49:03+00:00Added an answer on May 21, 2026 at 3:49 am

      Here is a complete rework of the regex in C#.

      Assumptions : (tell me if one of them is false or all are false)

      1. An INI file section can only have key/value pair lines in its body
      2. In an non INI file section, function calls can’t have any parameters

      Regex flags :
      RegexOptions.IgnoreCase | RegexOptions.IgnorePatternWhitespace | RegexOptions.Compiled | RegexOptions.Singleline


      Input test:

      
      const string dataFileScr = @"
      Start 0
      {
          Next = 1
          Author = rk
          Date = 2011-03-10
      /*  Description = simple */
      }
      
      PZ 11
      {
      IA_return()
      }
      
      GDC 7
      {
          Message = 6
          Message = 7
              Message = 8
              Message = 8
          RepeatCount = 2
          ErrorMessage = 10
          ErrorMessage = 11
          onKey[5] = 6
          onKey[6] = 4
          onKey[9] = 11
      }
      
      [Section1]
      key1 = value1
      key2 = value2
      
      [Section2]
      key1 = value1
      key2 = value2
      ";
      

      Reworked regex:

      
      const string patternFileScr = @"
      (?<Section>                                                              (?# Start of a non ini file section)
        (?<SectionName>[\w ]+)\s*                                              (?# Capture section name)
           {                                                                   (?# Match but don't capture beginning of section)
              (?<SectionBody>                                                  (?# Capture section body. Section body can be empty)
               (?<SectionLine>\s*                                              (?# Capture zero or more line(s) in the section body)
               (?:                                                             (?# A line can be either a key/value pair, a comment or a function call)
                  (?<KeyValuePair>(?<Key>[\w\[\]]+)\s*=\s*(?<Value>[\w-]*))    (?# Capture key/value pair. Key and value are sub-captured separately)
                  |
                  (?<Comment>/\*.+?\*/)                                        (?# Capture comment)
                  |
                  (?<FunctionCall>[\w]+\(\))                                   (?# Capture function call. A function can't have parameters though)
               )\s*                                                            (?# Match but don't capture white characters)
               )*                                                              (?# Zero or more line(s), previously mentionned in comments)
              )
           }                                                                   (?# Match but don't capture beginning of section)
      )
      |
      (?<Section>                                                              (?# Start of an ini file section)
        \[(?<SectionName>[\w ]+)\]                                             (?# Capture section name)
        (?<SectionBody>                                                        (?# Capture section body. Section body can be empty)
           (?<SectionLine>                                                     (?# Capture zero or more line(s) in the section body. Only key/value pair allowed.)
              \s*(?<KeyValuePair>(?<Key>[\w\[\]]+)\s*=\s*(?<Value>[\w-]+))\s*  (?# Capture key/value pair. Key and value are sub-captured separately)
           )*                                                                  (?# Zero or more line(s), previously mentionned in comments)
        )
      )
      ";
      

      Discussion
      The regex is build to match either non INI file sections (1) or INI file section (2).

      (1) Non-INI file sections These sections are composed by a section name followed by a body enclosed by { and }.
      The section name con contain either letters, digits or spaces.
      The section body is composed by zero or more lines. A line can be either a key/value pair (key = value), a comment (/* Here is a comment */) or a function call with no parameters (my_function()).

      (2) INI file sections
      These sections are composed by a section name enclosed by [ and ] followed by zero or more key/value pairs. Each pair is on one line.

      • 0
      • Reply
      • Share
        Share
        • Share on Facebook
        • Share on Twitter
        • Share on LinkedIn
        • Share on WhatsApp
        • Report

    Sidebar

    Related Questions

    First time stackoverflow user but occasional lurker, hope you guys can help me out.
    Hope you guys can help me out. I have information coming into a serial
    i have a problem so hope you guys can help! I have a string
    After an extensive unsuccessful search I hope you guys can help me out here...
    Hope you guys can help me out once more before I go nuts. Iv'e
    I am a newbie with hibernate I hope you guys can help me out
    the following codes are what i'm trying to figure out.Hope you guys can help
    I'm really confused so I hope you guys can help me. I know how
    I hope some of you guys can help me with this problem.... I have
    Just want to start out by saying this seems like a great site, hope

    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.