Python 的 re.split() 没有删除所有匹配的字符

标签 python regex string parsing

这简直让我抓狂。我确信正则表达式匹配字符串开头的整个日期范围。然而,当我执行 re.split 时,一个 8 卡在后面。这是怎么回事,我如何在该日期范围内拆分(在某些情况下,它可能在字符串的开头和中间,因此拆分)?

import re
a = "09/05/2018-12/18/2018 Lecture Wednesday 01:30PM - 02:45PM, Room to be Announced"
b = r"([0-9]|\/|-){21}"
print re.split(b, a)

结果

['', '8', ' Lecture Wednesday 01:30PM - 02:45PM, Room to be Announced']

最佳答案

来自 re.split 的文档:

If capturing parentheses are used in pattern, then the text of all groups in the pattern are also returned as part of the resulting list.

您确实有一个捕获组,它最后匹配的是字符 8。这就是返回 8 的原因。

您可以改用非捕获组:

>>> b = r"(?:[0-9]|\/|-){21}"
           ^^ note these two characters added
>>> re.split(b, a)
['', ' Lecture Wednesday 01:30PM - 02:45PM, Room to be Announced']

或者您可以将所有选择放在一个字符类中,根本不需要一个组:

>>> b = r"[-/0-9]{21}"
>>> re.split(b, a)
['', ' Lecture Wednesday 01:30PM - 02:45PM, Room to be Announced']

关于Python 的 re.split() 没有删除所有匹配的字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49583535/

相关文章:

用于捕获 {} 的正则表达式

c# - 字典值作为不同的键

java - 将 double 格式设置为分数

python - 分层注意力网络 - model.fit 生成错误 'ValueError: Input dimension mis-match'

python - 如何在保留顺序的同时从图表图像中获取数据?

python - 根据列值返回用户列表

Ruby 条件正则表达式变通方法

python - 带 turtle 图形的 14 段显示

javascript 替换为具有特定 id 的正则表达式 img 标记,但其他属性可以是任何内容

javascript - .toLowerCase 不起作用,替换功能?