Python's Re.split() Not Removing All Matched Characters
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. W
Solution 1:
From the docs for 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.
You do have a capturing group, and the last thing it matches is the character 8
. That's why 8
is returned.
You can use a non-capturing group instead:
>>> 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']
Post a Comment for "Python's Re.split() Not Removing All Matched Characters"