0

I am parsing an .ini file and I need to target an exact section. The .ini file looks like this and I am trying to target the section "2nd Section" and not continue on:

1st Section
    item1
    item2
    item3
2nd Section
    item1
    item2
    item3
3rd Section
    item1
    item2
    item3

Here is the code I have so far to target the section header "1st String"

    private static string ParseSataHeader(string sataHeader, string bcuFileName)
    {
        string strLine = "";
        StreamReader myReader = new StreamReader(bcuFileName);
        bool foundSection = false;

        while (strLine !=null)
        {
            strLine = myReader.ReadLine();
            if (strLine !=null)
            {
               if (String.Compare("2nd Section", strLine, StringComparison.OrdinalIgnoreCase) == 0)
                    sataHeader = strLine.Trim();
            }

In VBS i know that you can do a RegExpression function in order to keep iterating until you find a string that is not indented. Any ideas?

gunr2171
  • 16,104
  • 25
  • 61
  • 88
vipercity
  • 273
  • 1
  • 2
  • 11
  • you mean this http://regex101.com/r/hI8vY0/1 ? – Avinash Raj Sep 09 '14 at 17:25
  • try this http://regex101.com/r/dU4yE6/1 – vks Sep 09 '14 at 17:39
  • I had a somewhat similar task so I created a [MadMilkman.Ini](https://github.com/MarioZ/MadMilkman.Ini) library for this. I need to point out that you can actually retrieve an information about INI sections or items indentation for example: "file.Sections["2nd Section"].LeftIndentation" or "file.Sections["2nd Section"].Keys["item1"].LeftIndentation" – Mario Z Jan 31 '16 at 14:49

2 Answers2

1

Have a look at this INI parser written for C#. I believe it would be good if you use some parser instead of regex engine or some string split etc.

NeverHopeless
  • 11,077
  • 4
  • 35
  • 56
  • What, use a parser? [Why would you want to suggest that](http://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags)? +1 – gunr2171 Sep 09 '14 at 17:29
0

Using the ini-parser library is a piece of cake

// Parse the ini file
IniData parsedData = new FileIniDataParser.LoadFile("ini_file_path.ini");

// Get an specific value inside that section ...
var value = parsedData["2nd Section"]["item1"]

// ... or just iterate to get all values for that section
foreach(KeyData key in parsedData["2nd Section"].Keys)
   Console.WriteLine(key.Name + " = " + key.Value);

Disclaimer: I'm the creator of ini-parser

Ricardo Amores
  • 4,597
  • 1
  • 31
  • 45