问题描述
我正在尝试突出显示 Markdown 代码,但遇到了 .NET 正则表达式多行选项的这种奇怪行为.
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:
但它拒绝与 .net 一起使用:
But it refuses to work with .net:
它似乎没有考虑到 $ 标签,只是突出显示直到字符串末尾的所有内容,无论如何.这是我的 C#
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)
我错过了什么?
推荐答案
很明显,您的文本包含除 LF 之外的换行符.在 .NET 正则表达式中,点匹配除 LF 之外的任何字符(换行符,).
It is clear your text contains a linebreak other than LF. In .NET regex, a dot matches any char but LF (a newline char, ).
See Multiline Mode MSDN regex reference
默认情况下,$
只匹配输入字符串的结尾.如果您指定 RegexOptions.Multiline选项,它匹配换行符 () 或输入字符串的结尾.但是,它不匹配回车/换行字符组合.要成功匹配它们,请使用子表达式
?$
而不是 $
.
所以,使用
@"^(#+).+?
?$"
.+??$
将延迟匹配除 LF 之外的任何一个或多个字符,直到换行符之前的第一个 CR(这是可选的).
The .+??$
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:
@"^(#+)[^
]+"
[^]+
将匹配 CR/LF 以外的一个或多个字符.
The [^]+
will match one or more chars other than CR/LF.
这篇关于.Net 正则表达式匹配 $ 与字符串的结尾而不是行的结尾,即使启用了多行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!