using System;
using System.Text.RegularExpressions;
public class Example
{
public static void Main()
{
string pattern = @"(?:^|\n)##\s[^\n]*\n(.*?)(?=\n##?\s|$)";
string input = @"# This is an H1
content
content
content
## This is an H2 that ends in another H2
matched content 1
matched content 1
matched content 1
## This is an H2 that ends in an H1
matched content 2
matched content 2
matched content 2
# This is an H1, it's content doesn't belong to any H2
content
content
content
## This is an H2 that goes on until the end of the file
matched content 3
matched content 3
### This H3 belongs to the H2 above
matched content 4
matched content 4";
RegexOptions options = RegexOptions.Singleline;
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