Python正则表达式:使用量词进行前瞻 [英] Python regex: Following lookahead with quantifier

查看:168
本文介绍了Python正则表达式:使用量词进行前瞻的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

只是想让我为这个问题着想,当我正积极地向前看时,这个问题就发生了.

Just trying to wrap my mind around this question, which occurred to me while I was messing around with positive lookaheads.

此正则表达式有意义吗?

Does this regex make any sense?

foo(?=bar)+

re.match()不会返回错误,但是如果'+'量词有任何意义,我无法弄清楚它会是什么. (FWIW,regex101.com给出错误先前的令牌不可量化" ...)

re.match() doesn't return an error, but if there's any sense to the '+' quantifier, I can't figure what it would be. (FWIW, regex101.com gives the error 'The preceding token is not quantifiable' ...)

谢谢.

/约翰

推荐答案

这里没有理由使用+量词.正则表达式先行查找和后退实际上不匹配任何文本,这意味着如果它们在某个位置匹配一次,则它们将连续匹配"无数次.似乎python很聪明,不会多次尝试匹配lookahead,因为它不会陷入无限循环中.

There is no reason to use the + quantifier here. Regular expression lookaheads and lookbehinds don't actually match any text, which means that if they match once in a position they will "match" an infinite number of times in a row. It seems python is smart enough not to try matching the lookahead more than once, as it does not get caught up in an infinite loop.

换句话说,只要坚持不合格的前瞻:foo(?=bar)最好.

In other words, just stick with unqualified lookaheads: foo(?=bar) is best.

这篇关于Python正则表达式:使用量词进行前瞻的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆