please help with this case:
m = re.split('([A-Z][a-z]+)', 'PeopleRobots')
print (m)
Result:
['', 'People', '', 'Robots', '']
Why does the list have empty elements?
Definition and Usage. The split() method splits a string into a list. You can specify the separator, default separator is any whitespace.
split() Return ValueThe return value of the split() method is always a list of strings obtained after breaking the given string by the specified separator.
The re. split() function splits the given string according to the occurrence of a particular character or pattern. Upon finding the pattern, this function returns the remaining characters from the string in a list.
The split() method does not change the value of the original string. If the delimiter is an empty string, the split() method will return an array of elements, one element for each character of string. If you specify an empty string for string, the split() method will return an empty string and not an array of strings.
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']
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With