Python regex: Following lookahead with quantifier

◇◆丶佛笑我妖孽 提交于 2019-12-02 16:21:20

问题


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() 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' ...)

Thanks.

/John


回答1:


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.

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



来源:https://stackoverflow.com/questions/42881855/python-regex-following-lookahead-with-quantifier

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!