multi split function taking delimiter list
Peter Otten
__peter__ at web.de
Tue Nov 14 15:17:33 EST 2006
martinskou at gmail.com wrote:
> Hi, I'm looking for something like:
>
> multi_split( 'a:=b+c' , [':=','+'] )
>
> returning:
> ['a', ':=', 'b', '+', 'c']
>
> whats the python way to achieve this, preferably without regexp?
I think in this case the regexp approach is the simplest, though:
>>> def multi_split(text, splitters):
... return re.split("(%s)" % "|".join(re.escape(splitter) for splitter
in splitters), text)
...
>>> multi_split("a:=b+c", [":=", "+"])
['a', ':=', 'b', '+', 'c']
Peter
More information about the Python-list
mailing list