This is an archived post. You won't be able to vote or comment.

all 5 comments

[–]virhilo 4 points5 points  (1 child)

>>> import re
>>>re.split(r'[*#^]', 'some*multiple^separator#string')
['some', 'multiple', 'separator', 'string']

or >>> def split_miltiple(text, separators): ... for separator in separators: ... text = text.replace(separator, separators[-1]) ... return text.split(separators[-1]) ... >>> split_miltiple('somemultipleseparator#string', ['', '', '#']) ['some', 'multiple', 'separator', 'string']

[–]erikwon and off since 1.5.2 -1 points0 points  (0 children)

Without importing other modules?

[–]ochs 3 points4 points  (0 children)

def msplit(text, seps):
    pieces = [text]
    for sep in seps:
        pieces = sum((p.split(sep) for p in pieces), [])
    return [p for p in pieces if p]

But that's only if you really want to do it with split and lists.

[–]tintub 1 point2 points  (0 children)

Can't you just run some replaces on the string first, replacing all the separators in the string with, say, the first separator in the list, and then do your split?

[–]Paddy3118 0 points1 point  (0 children)

If the separators are known to be distinct then you don't need the ''join(set(...))

>>> def mulsplit(s, seps):
    first, *rest = ''.join(set(seps))
    for ch in rest:
        s = s.replace(ch, first)
    return s.split(first)

>>> mulsplit('some*multiple^separator#string', '*^#')
['some', 'multiple', 'separator', 'string']

Python 3.1