Python正则表达式奇怪的行为 [英] Python regex strange behavior

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

问题描述

我发现了一些我无法在 Python re 模块中解释的东西.编译 (a*)*(a*|b)* 会抛出错误:

I've discovered something that I can't explain in Python re module. Compilation of (a*)* or (a*|b)* throws an error:

raise error, v # 无效的表达式sre_constants.error: 没什么可重复的

raise error, v # invalid expression sre_constants.error: nothing to repeat

我已经在 javascript 中测试了这个正则表达式,它似乎没问题.

I've tested this regexp in javascript and it seems to be ok.

这是一个错误吗?

推荐答案

是的,这是一个错误(或者至少是一个错误功能).它抱怨如果 a* 什么都不匹配,它就不知道如何捕获 0 个或多个无".

Yes, it's a bug (or at least a misfeature). It's complaining that if a* matches nothing, it doesn't know how to capture 0 or more "nothings".

这篇关于Python正则表达式奇怪的行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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