如何在正则表达式中查找不包含字符串的匹配项

How to find matches that do not include a string in regular expressions

我正在尝试创建一个匹配 #rn: 的正则表达式,但如果它后面跟有 msg,则不会。

所以

#rn:abc -> matches
#rn:msh -> matches
#rn:xy -> matches
#rn:defg -> matches
#rn:msg - does not match

我试过 \#rn\:[^m][^s][^g]\#rn\:[^(msg)] 但它们不起作用。

能做到吗?

\#rn\:(?!msg)

这应该为 you.Here 做 lookahead 确保在 #rn: 之后没有 msg。参见演示。

https://regex101.com/r/tX2bH4/28