Python的re.split()不会删除所有匹配的字符 [英] Python's re.split() not removing all matched characters
问题描述
这真让我发疯.我肯定正则表达式会匹配字符串开头的整个日期范围.但是,当我重新拆分时,8
会挂在后面.
This is driving me absolutely nuts. I am positive that the entire date range at the start of the string is being matched by the regex. Yet, when I do re.split, an 8
hangs behind. What's going on here and how can I split on that date range (in some cases it might be at the start and in the middle of the string, hence the split)?
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
的原因.
You do have a capturing group, and the last thing it matches is the character 8
. That's why 8
is returned.
您可以改用非捕获组:
>>> b = r"(?:[0-9]|\/|-){21}"
^^ note these two characters added
>>> re.split(b, a)
['', ' Lecture Wednesday 01:30PM - 02:45PM, Room to be Announced']
或者您可以将所有选择放在一个字符类中,而根本不需要一个组:
Or you could put all the choices in a single character class, and not need a group at all:
>>> b = r"[-/0-9]{21}"
>>> re.split(b, a)
['', ' Lecture Wednesday 01:30PM - 02:45PM, Room to be Announced']
这篇关于Python的re.split()不会删除所有匹配的字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!