Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

A RegEx grouping challenge

Status
Not open for further replies.

BuGlen

MIS
Jun 16, 2000
254
0
0
US
I'm studying RegEx patterns for use in C# and I'm trying to find out if a subgroup scenerio is possible. I'm parsing an INI file and I'd like to return a single matches collection that contains the section, key, and value information for each entry. Here's what I have for patterns so far:

Full Section Text: (?isx-mn)\[.*?(?=\[|\z)
Section Label (Named): (?<Section>\[.*\])
Key-Value Pairs (Named): (?<Key>.*)\s*=\s*(?<Value>.*)(?=\r)

What I can't figure out is how to create a pattern that will return a collection relating the key-value pairs to the section. Since a section can contain several key-value pairs (essentially, a one-to-many relationship), I'm not even sure this can be accomplished with a single pattern.

The results I'm hoping to acheive is a flat structure:

[Section][Key][Value]
[Section][Key][Value]
...

If anyone has done this type of grouping before, or feels up to the challenge, I would definately appreciate the help.

BTW, if you use RegEx and you haven't tried Expresso yet, you might want to check it out. It's a great (free) tool for evaluating expressions.
- Glen
 
can you post a short example of how the ini file looks and how you would like it to be transformed?

--------------------------
"two wrongs don't make a right, but three lefts do" - the unknown sage
 
Absolutely, and thanks.

Your standard INI file is formatted with key-value pairs in sections like this:

[Section One]
Key1=Value1
Key2=Value2
Key3=Value3

[Section Two]
Key4=Value4
Key5=Value5
...

I would like to retrieve the data like this (if it's possible), using named groups:

[Section][Key][Value]
Section One, Key1, Value1
Section One, Key2, Value2
Section One, Key3, Value3
Section Two, Key4, Value4
Section Two, Key5, Value5
...

As you can see, the difficult part is managing the relationship between the sections and the key-value pairs in the expression. There's also a possible condition where the first group does not have a section identifier, but I'm not too concerned about that right now.

If you look in the C:\Windows folder of any XP box, you should see a good number of INI files, many of which fall into the grouping I illustrated above.

Thanks in advance for any insight you can provide.

Hope this helps.

- Glen

Know thy data.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top