Okay, so I’m working with html and I want to match everything between two comments generated by a CMS – including linebreaks.
Example:
<!-- Start Magic -->
<h2>My title</h2>
<p>Here's some content</p>
<p>And hey look, a linebreak!
And here's another for good measure!
</p>
<!-- End Magic -->
And here’s the Regex I’m using to extract the guts:
Regex.Match(magic, @"<!-- Start Magic -->(?<guts>[\s\S]*?)<!-- End Magic -->");
Now I should note that this actually works fine. I just wondered whether using [\s\S]*? is the best way of matching everything (including line breaks) in a non-greedy fashion.
There is another method using the RegexOptions shown below:
With RegexOptions.SingleLine you are informing the C# regex engine to change the meaning of dot so that it matches every character (instead of the default which is every character excluding \n)
This doesn’t address “the best way” of doing this since that is rather subjective, including considerations like performance and readability.