请帮忙解决这个问题:
m = re.split('([A-Z][a-z]+)', 'PeopleRobots')
print (m)
结果:
['', 'People', '', 'Robots', '']
为什么列表中有空元素?
最佳答案
If there are capturing groups in the separator and it matches at the start of the string, the result will start with an empty string. The same holds for the end of the string:
如果你想得到People
和Robots
,使用re.findall :
>>> re.findall('([A-Z][a-z]+)', 'PeopleRobots')
['People', 'Robots']
您可以省略分组:
>>> re.findall('[A-Z][a-z]+', 'PeopleRobots')
['People', 'Robots']
关于python - re.split() 给出列表中的空元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18124254/