using System;
using System.Text.RegularExpressions;
public class Example
{
public static void Main()
{
string pattern = @"{\s*{\s*#\s*(?<operator>endcase|endswitch|endfor|endif|if|elif|else|for|switch|case|default)\s*(?<args>[^}]*)\s*}\s*}\s*(?<body>[^{]*(?:\s*{\s*{\s*#\s*(?!(?&operator))\s*[^}]*\s*}\s*}\s*[^{]*)*)";
string input = @"<div>
{{#if arg1.arg2}}
<span>{{#var arg3 static}}</span>
blablabla{{#for k, v in arg4}}
<p>{{#var k}}<span>{{#var v.arg5}}</span></p>
{{#if (k == ""test"" or v != 0) and k == 0}}
<span></span>
{{#elif k == 0}}
<p></p>
{{#else}}
<div></div>
{{#endif}}
{{#endfor}}
{{#endif}}
{{#switch arg6}}
{{#case 5}}
case 5
{{#case 6, 7, 8}}
case 6, 7, 8
{{#endcase}}
{{#default}}
def
{{#endcase}}
{{#endswitch}}
{{#for i as 0, 5}}
test i
{{#endfor}}";
RegexOptions options = RegexOptions.CultureInvariant | 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