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

后端 未结 2 1943
名媛妹妹
名媛妹妹 2020-11-29 12:56

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

The following expression: ^(#+).+$ w

2条回答
  •  醉酒成梦
    2020-11-29 13:16

    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.

提交回复
热议问题