Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

.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?

like image 283
user2950509 Avatar asked Oct 15 '16 11:10

user2950509


People also ask

How to use regex to match a line in a string?

1 Line Anchors In regex, anchors are not used to match characters. Rather they match a position i.e. ... 2 Regex patterns to match start of line Description Matching Pattern Line starts with number “^\\d” or “^ [0-9]” Line starts with character “^ [a-z]” or “^ [A-Z]” Line starts ... 3 Regex patterns to match end of line

How to match start and end of line in a string?

To match start and end of line, we use following anchors: Caret (^) matches the position before the first character in the string. Dollar ($) matches the position right after the last character in the string. 2. Regex patterns to match start of line

How to match the position after the last character in regex?

To match the position after the last character of any line, we must enable the multi-line mode in the regular expression. In this case, dollar changes from matching at only the last the entire string to the last of any line within the string. Program output. Drop me your questions related to programs for regex starts with and ends with java.

How do I get the second match of a regex query?

Match match = Regex.Match (value, @"\d"); if (match.Success) { Console.WriteLine (match.Value); } // Get second match. match = match. NextMatch (); if (match.Success) { Console.WriteLine (match.Value); } } } Output 4 5


1 Answers

It is clear your text contains a linebreak other than LF. In .NET regex, a dot matches any char but LF (a newline char, \n).

See Multiline Mode MSDN regex reference

By default, $ matches only the end of the input string. If you specify the RegexOptions.Multiline option, it matches either the newline character (\n) or the end of the input string. It does not, however, match the carriage return/line feed character combination. To successfully match them, use the subexpression \r?$ instead of just $.

So, use

@"^(#+).+?\r?$"

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:

@"^(#+)[^\r\n]+"

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

like image 134
Wiktor Stribiżew Avatar answered Sep 28 '22 04:09

Wiktor Stribiżew