重新分割()给出lis中的空元素

2024-04-24 07:18:06 发布

您现在位置:Python中文网/ 问答频道 /正文

请帮忙处理这个案子:

m = re.split('([A-Z][a-z]+)', 'PeopleRobots')
print (m)

结果:

^{pr2}$

为什么列表中有空元素?在


Tags: re元素列表splitprint案子pr2peoplerobots
1条回答
网友
1楼 · 发布于 2024-04-24 07:18:06

根据re.split documentation

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:

如果要获得PeopleRobots,请使用re.findall

>>> re.findall('([A-Z][a-z]+)', 'PeopleRobots')
['People', 'Robots']

可以省略分组:

^{pr2}$

相关问题 更多 >