<p>I want to parse a config file sorta thing, like so:</p>

<pre><code>[KEY:Value]     
    [SUBKEY:SubValue]
</code></pre>

<p>Now I started with a <code>StreamReader</code>, converting lines into character arrays, when I figured there's gotta be a better way. So I ask you, humble reader, to help me.</p>

<p>One restriction is that it has to work in a Linux/Mono environment (1.2.6 to be exact). I don't have the latest 2.0 release (of Mono), so try to restrict language features to C# 2.0 or C# 1.0.</p>

[{'answer_id': 13974, 'author': 'Ed S.', 'author_id': 1053, 'author_profile': 'https://Stackoverflow.com/users/1053', 'pm_score': 0, 'selected': False, 'text': '<p>It looks to me that you would be better off using an XML based config file as there are already .NET classes which can read and store the information for you relatively easily.  Is there a reason that this is not possible?</p>\n\n<p>@Bernard:  <del>It is true that hand editing XML is tedious, but the structure that you are presenting already looks very similar to XML.</del></p>\n\n<p>Then yes,  has a good method there.  </p>\n'}, {'answer_id': 13975, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': 0, 'selected': False, 'text': '<p>You can also use a stack, and use a push/pop algorithm. This one matches open/closing tags.</p>\n\n<pre><code>public string check()\n    {\n        ArrayList tags = getTags();\n\n\n        int stackSize = tags.Count;\n\n        Stack stack = new Stack(stackSize);\n\n        foreach (string tag in tags)\n        {\n            if (!tag.Contains(\'/\'))\n            {\n                stack.push(tag);\n            }\n            else\n            {\n                if (!stack.isEmpty())\n                {\n                    string startTag = stack.pop();\n                    startTag = startTag.Substring(1, startTag.Length - 1);\n                    string endTag = tag.Substring(2, tag.Length - 2);\n                    if (!startTag.Equals(endTag))\n                    {\n                        return "Fout: geen matchende eindtag";\n                    }\n                }\n                else\n                {\n                    return "Fout: geen matchende openeningstag";\n                }\n            }\n        }\n\n        if (!stack.isEmpty())\n        {\n            return "Fout: geen matchende eindtag";\n        }            \n        return "Xml is valid";\n    }\n</code></pre>\n\n<p>You can probably adapt so you can read the contents of your file. Regular expressions are also a good idea.</p>\n'}, {'answer_id': 13983, 'author': 'eplawless', 'author_id': 1370, 'author_profile': 'https://Stackoverflow.com/users/1370', 'pm_score': 2, 'selected': False, 'text': '<p>I was looking at almost this exact problem the other day: <a href="http://trackerrealm.com/blogs/2007/04/tokenize-string-with-c-regular.html" rel="nofollow noreferrer">this article</a> on string tokenizing is exactly what you need. You\'ll want to define your tokens as something like:</p>\n\n<pre><code>@"(?&amp;ltlevel&gt;\\s) | " +\n@"(?&amp;ltterm&gt;[^:\\s]) | " +\n@"(?&amp;ltseparator&gt;:)"\n</code></pre>\n\n<p>The article does a pretty good job of explaining it. From there you just start eating up tokens as you see fit.</p>\n\n<p>Protip: For an <a href="http://en.wikipedia.org/wiki/LL_parser" rel="nofollow noreferrer">LL(1) parser</a> (read: easy), tokens cannot share a prefix. If you have <code>abc</code> as a token, you cannot have <code>ace</code> as a token</p>\n\n<p>Note: The article\'s missing the | characters in its examples, just throw them in.</p>\n'}, {'answer_id': 13990, 'author': 'Orion Edwards', 'author_id': 234, 'author_profile': 'https://Stackoverflow.com/users/234', 'pm_score': 5, 'selected': True, 'text': '<blockquote>\n  <p>I considered it, but I\'m not going to use XML. I am going to be writing this stuff by hand, and hand editing XML makes my brain hurt. :\')</p>\n</blockquote>\n\n<p>Have you looked at <a href="http://www.yaml.org/" rel="noreferrer">YAML</a>?</p>\n\n<p>You get the benefits of XML without all the pain and suffering. It\'s used extensively in the ruby community for things like config files, pre-prepared database data, etc</p>\n\n<p>here\'s an example</p>\n\n<pre><code>customer:\n  name: Orion\n  age: 26\n  addresses:\n    - type: Work\n      number: 12\n      street: Bob Street\n    - type: Home\n      number: 15\n      street: Secret Road\n</code></pre>\n\n<p>There appears to be a <a href="http://yaml-net-parser.sourceforge.net/default.html" rel="noreferrer">C# library here</a>, which I haven\'t used personally, but yaml is pretty simple, so "how hard can it be?" :-)</p>\n\n<p>I\'d say it\'s preferable to inventing your own ad-hoc format (and dealing with parser bugs)</p>\n'}, {'answer_id': 14057, 'author': 'ICR', 'author_id': 214, 'author_profile': 'https://Stackoverflow.com/users/214', 'pm_score': 1, 'selected': False, 'text': '<p>Using a library is almost always preferably to rolling your own. Here\'s a quick list of "Oh I\'ll never need that/I didn\'t think about that" points which will end up coming to bite you later down the line:</p>\n\n<ul>\n<li>Escaping characters. What if you want a : in the key or ] in the value?</li>\n<li>Escaping the escape character.</li>\n<li>Unicode</li>\n<li>Mix of tabs and spaces (see the problems with Python\'s white space sensitive syntax)</li>\n<li>Handling different return character formats</li>\n<li>Handling syntax error reporting</li>\n</ul>\n\n<p>Like others have suggested, YAML looks like your best bet.</p>\n'}, {'answer_id': 14274, 'author': 'Gishu', 'author_id': 1695, 'author_profile': 'https://Stackoverflow.com/users/1695', 'pm_score': -1, 'selected': False, 'text': "<p>Regardless of the persisted format, using a Regex would be the fastest way of parsing.\nIn ruby it'd probably be a few lines of code.</p>\n\n<pre><code>\\[KEY:(.*)\\] \n\\[SUBKEY:(.*)\\]\n</code></pre>\n\n<p>These two would get you the Value and SubValue in the first group. Check out MSDN on how to match a regex against a string.</p>\n\n<p>This is something everyone should have in their kitty. Pre-Regex days would seem like the Ice Age.</p>\n"}, {'answer_id': 14292, 'author': 'ICR', 'author_id': 214, 'author_profile': 'https://Stackoverflow.com/users/214', 'pm_score': 0, 'selected': False, 'text': "<p>@Gishu</p>\n\n<p>Actually once I'd accommodated for escaped characters my regex ran slightly slower than my hand written top down recursive parser and that's without the nesting (linking sub-items to their parents) and error reporting the hand written parser had.</p>\n\n<p>The regex was a slightly faster to write (though I do have a bit of experience with hand parsers) but that's without good error reporting. Once you add that it becomes slightly harder and longer to do.</p>\n\n<p>I also find the hand written parser easier to understand the intention of. For instance, here is the a snippet of the code:</p>\n\n<pre><code>private static Node ParseNode(TextReader reader)\n{\n    Node node = new Node();\n    int indentation = ParseWhitespace(reader);\n    Expect(reader, '[');\n    node.Key = ParseTerminatedString(reader, ':');\n    node.Value = ParseTerminatedString(reader, ']');\n}\n</code></pre>\n"}, {'answer_id': 29192, 'author': 'Antoine Aubry', 'author_id': 2680, 'author_profile': 'https://Stackoverflow.com/users/2680', 'pm_score': 1, 'selected': False, 'text': '<p>There is <a href="http://yamldotnet.wiki.sourceforge.net/" rel="nofollow noreferrer">another YAML library for .NET</a> which is under development. Right now it supports reading YAML streams and has been tested on Windows and Mono. Write support is currently being implemented.</p>\n'}]
['https://Stackoverflow.com/questions/13963', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/61/']