python equiv of perl's split?

Pedro RODRIGUEZ pedro_rodriguez at club-internet.fr
Mon Jan 13 10:10:37 EST 2003


On Mon, 13 Jan 2003 15:24:39 +0100, pete-temp-12-29-2002 wrote:

> 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?
 
>>> import re
>>> re.split("(:)", "test:one:two:three")
['test', ':', 'one', ':', 'two', ':', 'three']
>>>

Pedro




More information about the Python-list mailing list