遇到字符序列时停止匹配:修复lookbehind

发布于 2024-08-27 18:07:20 字数 272 浏览 8 评论 0原文

我有以下正则表达式:

(?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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

偷得浮生 2024-09-03 18:07:20

您只在匹配结束时检查环视,这意味着它可以首先匹配尽可能多的字符串,然后再检查环视。由于字符串末尾没有 [[ ,因此匹配成功。

如果您确实想在此处使用正则表达式,则需要做的是检查您添加的每个字符的环视,如下所示:

>>> s = 'hello world! [[A string typically used in programming examples]]'
>>> regex = re.compile('(?P<question>((?!\[\[).)+)')
>>> regex.match(s).group('question')
'hello world! '

但请注意,仅使用 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:

>>> s = 'hello world! [[A string typically used in programming examples]]'
>>> regex = re.compile('(?P<question>((?!\[\[).)+)')
>>> regex.match(s).group('question')
'hello world! '

But note that it would be much easier just to use something like s.find('[[') instead of regular expressions here.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文