def multi_split(text, regexes):
def make_regex(s):<EOL><INDENT>return re.compile(s) if isinstance(s, basestring) else s<EOL><DEDENT>regexes = [make_regex(r) for r in regexes]<EOL>piece_list = [text]<EOL>finished_pieces = set()<EOL>def apply_re(regex, piece_list):<EOL><INDENT>for piece in piece_list:<EOL><INDENT>if piece in finished_pieces:<EOL><INDENT>yield piece<EOL>continue<EOL><DEDENT>for s in full_split(piece, regex):<EOL><INDENT>if regex.match(s):<EOL><INDENT>finished_pieces.add(s)<EOL><DEDENT>if s:<EOL><INDENT>yield s<EOL><DEDENT><DEDENT><DEDENT><DEDENT>for regex in regexes:<EOL><INDENT>piece_list = list(apply_re(regex, piece_list))<EOL>assert '<STR_LIT>'.join(piece_list) == text<EOL><DEDENT>return piece_list<EOL>
Split the text by the given regexes, in priority order.

Make sure that the regex is parenthesized so that matches are returned in
re.split().

Splitting on a single regex works like normal split.
>>> '|'.join(multi_split('one two three', [r'\w+']))
'one| |two| |three'

Splitting on digits first separates the digits from their word
>>> '|'.join(multi_split('one234five 678', [r'\d+', r'\w+']))
'one|234|five| |678'

Splitting on words first keeps the word with digits intact.
>>> '|'.join(multi_split('one234five 678', [r'\w+', r'\d+']))
'one234five| |678'