python - * 在 Python 正则表达式匹配中有副作用吗?

标签 python regex

我正在学习 Python 的正则表达式,以下是我预期的工作方式:

>>> import re
>>> re.split('\s+|:', 'find   a str:s2')
['find', 'a', 'str', 's2']

但是当我将 + 更改为 * 时,输出对我来说很奇怪:

>>> re.split('\s*|:', 'find  a str:s2')
['find', 'a', 'str:s2']

这种模式在 Python 中是如何解释的?

最佳答案

您看到的“副作用”是 re.split() 只会在超过 0 个字符的匹配项上拆分。

\s*|: 模式在零个或多个空格上匹配 either,在 : 上匹配以先到者为准。但是零空格到处匹配。在超过零个空格匹配的那些位置,进行拆分。

因为 \s* 模式在每次考虑拆分字符时都匹配,所以永远不会考虑下一个选项 :

非空匹配的拆分是 called out explicitly in the re.split() documentation :

Note that split will never split a string on an empty pattern match.

如果反转模式,:会被考虑,因为它是首选:

>>> re.split(':|\s*', 'find  a str:s2')
['find', 'a', 'str', 's2']

关于python - * 在 Python 正则表达式匹配中有副作用吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24389803/

相关文章:

python - Flask - 不受当前上下文的限制

python - 使用 python 正则表达式编辑二进制文件/字符串

java - 替换前导零

javascript - 无法创建正则表达式

java - 使用正则表达式从电子邮件中提取姓名

python - 使用 Folium 在 map 上的新图层中打印一条线/多线

python - Pandas 在非重叠列上展平层次索引

python - pandas:使用 to_latex 创建标题?

python - Django 的受限数据库用户

java - 正则表达式,以便只能输入 'a' ,'A' ,'p' 和 'P' 作为输入