遇到字符序列时停止匹配:修复lookbehind
我有以下正则表达式:
(?P<question>.+(?<!\[\[))
它旨在匹配字符串 hello world! 中的
hello world!
[[编程示例中通常使用的字符串]]
但我只是匹配整个字符串,而且我不明白为什么。我已经尝试了所有风格的环视,但它就是行不通......
有人知道如何解决这个问题吗?
谢谢,
金融理财师。
I have the following regexp:
(?P<question>.+(?<!\[\[))
It is designed to match hello world!
in the string hello world! [[A string typically used in programming examples]]
Yet I just matches the whole string, and I can't figure out why. I've tried all flavors of lookaround, but it just won't work...
Anyone knows how to fix this problem?
Thanks,
CFP.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您只在匹配结束时检查环视,这意味着它可以首先匹配尽可能多的字符串,然后再检查环视。由于字符串末尾没有
[[
,因此匹配成功。如果您确实想在此处使用正则表达式,则需要做的是检查您添加的每个字符的环视,如下所示:
但请注意,仅使用
s.find('[这里使用 [')
而不是正则表达式。You're only checking the lookaround at the end of the match, which means it can match as much as possible of the string first, and then afterwards check the lookaround. Since you don't have
[[
at the end of the string, the match succeeds.What you need to do if you really want to use regular expressions here is to check the lookaround for every character you add, like this:
But note that it would be much easier just to use something like
s.find('[[')
instead of regular expressions here.