Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
下面的正则表达式应该只匹配不以 # 字符开头后跟任何内容的行。
^[^#].*
但是如果缓冲区在它匹配下一行之前包含一个空行,即使它以 # 开头。
对于以下输入,它失败
This line is matched as expected # this line should not be matched, but it does if the above line is empty !?
你可以像这样修复它:
^[^#\r\n].*
您的原始表达式的问题^[^#].*在于[^#]匹配换行符(空行),因此允许点.匹配空行之后的整行,因此点实际上不匹配换行符,[^#]就是这样做的人.
[^#]
.
正则表达式 101 演示