using System;
using System.Text.RegularExpressions;
public class Example
{
public static void Main()
{
string pattern = @"(?=\S)(?<text>(?<comment>(?<open>[#;]+)(?:[^\S\r\n]*)(?<value>.+))|(?<section>(?<open>\[)(?:\s*)(?<value>[^\]]*\S+)(?:[^\S\r\n]*)(?<close>\]))|(?<entry>(?<key>[^=\r\n\[\]]*\S)(?:[^\S\r\n]*)(?<delimiter>:|=)(?:[^\S\r\n]*)(?<value>[^#;\r\n]*))|(?<undefined>.+))(?<=\S)|(?<linebreaker>\r\n|\n)|(?<whitespace>[^\S\r\n]+)";
string input = @"; Comment above the section
Key0=Value0
[Section 1]
Key=Value
[EmptySection]
Dummy text
;Commented Key=Value
;The following empty lines are ignored
;
#
[ Section 2 ]#Comment after [section];
Key 1 = Value 1
Key2 = Value 2 and [text in square brackets]
EmptyKey=
[];Noname section ignored
[ Section2 ] Dummy text after section
Key3 =Value 3 (symbols [;] are included)
#Key4 = Value 4 has been commented
Dummy text before section [ Sectionnum3 ]
Key 5 = Value 5
Key6 =Value6 ";
RegexOptions options = RegexOptions.IgnoreCase;
foreach (Match m in Regex.Matches(input, pattern, options))
{
Console.WriteLine("'{0}' found at index {1}.", m.Value, m.Index);
}
}
}
Please keep in mind that these code samples are automatically generated and are not guaranteed to work. If you find any syntax errors, feel free to submit a bug report. For a full regex reference for C#, please visit: https://msdn.microsoft.com/en-us/library/system.text.regularexpressions.regex(v=vs.110).aspx