Split string with multiple delimiters in Python

Split string with multiple delimiters in Python

Luckily, Python has this built-in 🙂

import re
re.split(; |, ,str)

Update:
Following your comment:

>>> a=Beautiful, is; better*thannugly
>>> import re
>>> re.split(; |, |*|n,a)
[Beautiful, is, better, than, ugly]

Do a str.replace(; , , ) and then a str.split(, )

Split string with multiple delimiters in Python

Heres a safe way for any iterable of delimiters, using regular expressions:

>>> import re
>>> delimiters = a, ..., (c)
>>> example = stackoverflow (c) is awesome... isnt it?
>>> regexPattern = |.join(map(re.escape, delimiters))
>>> regexPattern
a|\.\.\.|\(c\)
>>> re.split(regexPattern, example)
[st, ckoverflow ,  is , wesome,  isnt it?]

re.escape allows to build the pattern automatically and have the delimiters escaped nicely.

Heres this solution as a function for your copy-pasting pleasure:

def split(delimiters, string, maxsplit=0):
    import re
    regexPattern = |.join(map(re.escape, delimiters))
    return re.split(regexPattern, string, maxsplit)

If youre going to split often using the same delimiters, compile your regular expression beforehand like described and use RegexObject.split.


If youd like to leave the original delimiters in the string, you can change the regex to use a lookbehind assertion instead:

>>> import re
>>> delimiters = a, ..., (c)
>>> example = stackoverflow (c) is awesome... isnt it?
>>> regexPattern = |.join((?<={}).format(re.escape(delim)) for delim in delimiters)
>>> regexPattern
(?<=a)|(?<=\.\.\.)|(?<=\(c\))
>>> re.split(regexPattern, example)
[sta, ckoverflow (c),  is a, wesome...,  isnt it?]

(replace ?<= with ?= to attach the delimiters to the righthand side, instead of left)

Leave a Reply

Your email address will not be published. Required fields are marked *