我刚刚介绍了re
模块,我了解到它可以用来很好地分割字符串。然后我写了下面的代码:
import re
print(re.split(r'[a-f]','abcdefghtuyi op re the'))
我预计Python会返回:
['ghtuyi op r', ' th',] # as all the alphabets from a to f are expected to be sliced
但它回来了:
['' '', '', '', '', '', 'ghtuyi op r', ' th', '']
为什么会出现这些空白列表对象?
另外,我确实看过这个问题:Why are empty strings returned in split() results?但它与re
无关。如何在使用re
模块时摆脱这些空白对象?