Article View: borland.public.csharpbuilder.language.csharp
Article #603Regular expressions to parse an INI file, anyone?
From: Marc Scheuner
Date: Mon, 28 Jun 2004 11:43
Date: Mon, 28 Jun 2004 11:43
44 lines
2388 bytes
2388 bytes
Folks,
I was trying to parse standard Windows INI files using regular
expressions, but I can't seem to get it to work all the way....
I have two tasks, basically:
* retrieve all the section names (names between [....])
* given a section, retrieve all its key=value strings
I can achieve goal no. 1 by having a regular expression of
"\[(?<section>(.*))\].*". This seems to work okay - I can load the INI
file into a string, and apply that regex to it, and iterating over all
the .Matches and looking at the Match.Groups["section"].Value entries,
I get my section names:
StreamReader oSR = new StreamReader(<name of my INI file>);
string strINIFile = oSR.ReadToEnd();
oSR.Close();
// grab all the section names by means of a regular expression
MatchCollection oMatches = Regex.Matches(strINIFile,
@"\[(?<section>(.*))\].*");
foreach(Match oMatch in oMatches)
{
string sConfSection = oMatch.Groups["section"].Value;
// do something with sConfSection
}
However, I can't seem to get any regular expression working that would
pick out all the key=value pairs for me, from a given section. I
figured I'd just search for the "[section]" token, and then grab
anything up to the next "[" character - but I can't seem to get that
working.....
Any ideas???
Marc
================================================================
Marc Scheuner May The Source Be With You!
Bern, Switzerland m.scheuner(at)inova.ch
Technical Details
Message-ID:
<o210e0p6e7ndrqh72tighb6bunr7c1vunk@4ax.com>Path:
arc1-ca.pugleaf.net!g2news1.google.com!news1.google.com!news.glorb.com!border1.nntp.dca.giganews.com!border2.nntp.dca.giganews.com!nntp.giganews.com!newsfeeds.ihug.co.nz!ihug.co.nz!newsgroups.borland.com!not-for-mailShow Full Headers
Complete Article Headers
X-Google-Thread: 1efe35,482ddbe19fa3a83b,start X-Google-Attributes: gid1efe35,public X-Google-Language: ENGLISH,ASCII-7-bit From: Marc Scheuner <no.spam@for.me> Newsgroups: borland.public.csharpbuilder.language.csharp Subject: Regular expressions to parse an INI file, anyone? Date: Mon, 28 Jun 2004 13:43:33 +0200 Message-ID: <o210e0p6e7ndrqh72tighb6bunr7c1vunk@4ax.com> X-Newsreader: Forte Agent 1.93/32.576 English (American) MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit NNTP-Posting-Host: 195.141.64.252 X-Trace: newsgroups.borland.com 1088423012 195.141.64.252 (28 Jun 2004 04:43:32 -0700) Lines: 44 Path: arc1-ca.pugleaf.net!g2news1.google.com!news1.google.com!news.glorb.com!border1.nntp.dca.giganews.com!border2.nntp.dca.giganews.com!nntp.giganews.com!newsfeeds.ihug.co.nz!ihug.co.nz!newsgroups.borland.com!not-for-mail