0

I want to split a string by one or more occurrences of space or comma. My first take was:

re.split(r'[,\s]+', s)

But when I give it a string like this, it returns empty strings at the start and end.

>>> re.split(r'[,\s]+', '  foo,  bar,  baz,')
['', 'foo', 'bar', 'baz', '']

How can I modify the pattern such that the empty results at the beginning and end go away?

I tried negative lookahead and lookbehind, but I couldn't get the results I want. Also, I prefer not to add extra code to filter out the empty strings if this can be done by the regular expression alone.

Can
  • 377
  • 2
  • 10

0 Answers0