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:
But it refuses to work with .net:
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?
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
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
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.
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
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With