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

Popular posts from this blog

matlab - Deleting rows with specific rules -

jquery - How would i go about shortening this code? And to cancel the previous click on click of new section? -