python - re.split() gives empty elements in list -
please case:
m = re.split('([a-z][a-z]+)', 'peoplerobots') print (m) result:
['', 'people', '', 'robots', ''] why list have empty elements?
according re.split documentation:
if there capturing groups in separator , matches at start of string, result start empty string. same holds the end of string:
if want people , 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']
Comments
Post a Comment