您好,欢迎来到图艺博知识网。
搜索
您的当前位置:首页python 正则表达式 前瞻,Python正则表达式:使用量词进行前瞻

python 正则表达式 前瞻,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() 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

解决方案

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.

因篇幅问题不能全部显示,请点此查看更多更全内容

Copyright © 2019- huatuoyibo.net 版权所有 湘ICP备2023021910号-2

违法及侵权请联系:TEL:199 1889 7713 E-MAIL:2724546146@qq.com

本站由北京市万商天勤律师事务所王兴未律师提供法律服务