13

I'm using regular expressions to split a string using multiple delimiters. But if two of my delimiters occur next to each other in the string, it puts an empty string in the resulting list. For example:

re.split(',|;', "This,is;a,;string")

Results in

['This', 'is', 'a', '', 'string']

Is there any way to avoid getting '' in my list without adding ,; as a delimiter?

David DeMar
  • 2,390
  • 2
  • 32
  • 45

1 Answers1

33

Try this:

import re
re.split(r'[,;]+', 'This,is;a,;string')
> ['This', 'is', 'a', 'string']
Óscar López
  • 232,561
  • 37
  • 312
  • 386