user2950509 user2950509 - 1 year ago 117
Markdown Question

.Net regex matching $ with the end of the string and not of line, even with multiline enabled

I'm trying to highlight markdown code, but am running into this weird behavior of the .NET regex multiline option.

The following expression:

works fine on any online regex testing tool:

enter image description here

But it refuses to work with .net:

enter image description here

It doesn't seem to take into account the $ tag, and just highlights everything until the end of the string, no matter what. This is my C#

RegExpression = new Regex(@"^(#+).+$", RegexOptions.Multiline)

What am I missing?

Answer Source

It is clear your text contains a linebreak other than LF. In .NET regex, a dot matches any char but LF. So, use


The .+?\r?$ will match lazily any one or more chars other than LF up to the first CR (that is optional) right before a newline.

Or just use a negated character class:


The [^\r\n]+ will match one or more chars other than CR/LF.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download