using System;
using System.Text.RegularExpressions;
public class Example
{
public static void Main()
{
string pattern = @"(?(DEFINE)
(?<quote>['""])
(?<pair>
(?>
\w+
|(?:
(?<pairQuote>(?"e))[^'""]+?\k<pairQuote>
)
)
\s*\:
)
(?<string>
(?<stringQuote>(?"e))[\S\s]*?(?<!\\)\k<stringQuote>
)
(?<integer>\-?\d+(?:\.\d+)?(?:e[-+]\d+)?)
(?<scalar>true|false|null|(?&integer))
(?<elements>
\s*(?&value)
(?(R&array)|(?>(?:\s*\,(?&elements))|\s*))
)
(?<array>\[(?>(?&elements)|\s*)\])
(?<value>
(?&object)
|(?&string)
|(?&scalar)
|(?&array)
)
(?<members>
\s*(?&pair)\s*(?&value)
(?(R&object)|(?>(?:\s*\,(?&members))|\s*))
)
(?<object>\{(?&members)?\})
)
\A(?&object)\Z";
string input = @"{""menu"": {
""header"": ""SVG Viewer"",
""items"": [
{""id"": ""Open""},
{""id"": ""OpenNew"", ""label"": ""Open New""},
null,
{""id"": ""ZoomIn"", ""label"": ""Zoom In""},
{""id"": ""ZoomOut"", ""label"": ""Zoom Out""},
{""id"": ""OriginalView"", ""label"": ""Original View""},
null,
{""id"": ""Quality""},
{""id"": ""Pause""},
{""id"": ""Mute""},
null,
{""id"": ""Find"", ""label"": ""Find...""},
{""id"": ""FindAgain"", ""label"": ""Find Again""},
{""id"": ""Copy""},
{""id"": ""CopyAgain"", ""label"": ""Copy Again""},
{""id"": ""CopySVG"", ""label"": ""Copy SVG""},
{""id"": ""ViewSVG"", ""label"": ""View SVG""},
{""id"": ""ViewSource"", ""label"": ""View Source""},
{""id"": ""SaveAs"", ""label"": ""Save As""},
null,
{""id"": ""Help""},
{""id"": ""About"", ""label"": ""About Adobe CVG Viewer...""}
],
""test"": [
""Some value"",
4,
null,{}
],
""empty_array"":[-0.34E+4]
}}";
RegexOptions options = RegexOptions.IgnoreCase | RegexOptions.Multiline | RegexOptions.IgnorePatternWhitespace;
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