How do you split a string in Python with multiple delimiters?

To do this using re.split you can do:

re.split(r'(>+|!+|]+)', string)

Explaining this briefly:

  • You split on one or more occurrences of the different delimiters (>!]).
  • In order to include the delimiters in the result, you put the pattern in a capturing group by putting parens around it.

Leave a Comment