re.split() gives empty elements in list

后端 未结 1 1773
情话喂你
情话喂你 2020-12-11 19:48

please help with this case:

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

Result:

[\'\', \'People\', \'\', \'         


        
相关标签:
1条回答
  • 2020-12-11 20:15

    According to 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:

    If you want to get People and Robots, use re.findall:

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

    You can omit grouping:

    >>> re.findall('[A-Z][a-z]+', 'PeopleRobots')
    ['People', 'Robots']
    
    0 讨论(0)
提交回复
热议问题