python equiv of perl's split?

Laura Creighton lac at strakt.com
Mon Jan 13 10:31:42 EST 2003


> Just wondering what the 'best' way of emulating perl's split() method
> when used in the following manner:
> 
>      split /(:)/
> 
> Basically, split the string using a colon as the separator.  NOTE: the
> parens indicate that the split function should *include* the colons in
> the returned list.  Thus, given the string:
> 
>      test:one:two:three
> 
> The following array (list) is returned:
> 
>      test
>      :
>      one
>      :
>      two
>      :
>      three
> 
> What would be the easiest way to emulate this in Python?
> 
> Thanks,
> Pete

This one is easy. :-)
>>> import re
>>> re.split("(:)", "test:one:two:three")
['test', ':', 'one', ':', 'two', ':', 'three']
>>> 
<wink>

Laura Creighton





More information about the Python-list mailing list