.NET Multiple Line Regex

Recently I was trying to use a Regular Expression in .NET. I was working with small blobs of text. I was trying to extract some code snippets out of some wiki markup.

The code snippets looked like:

{code:csharp|title=C#}
//C# Code
{code}
{code:vbnet|title=vb.net}
//VB.NET Code
{code}

For testing purposes I was using the simplest Regex I could think of {code:(.+?){code}. After playing around it became obvious that it was not matching over multiple lines. This makes sense because the ‘ .’ character  matches every character except /n/r

Well it turns out you want to enable SingleLineMode not MultiMode.  I know I know makes a ton of sense.

Lesson learned. Read the documentation.

Extra Bonus: If you want to quickly test .NET regular expressions with all these options. I found I like http://derekslager.com/blog/posts/2007/09/a-better-dotnet-regular-expression-tester.ashx

Leave a Reply