Split a string based on change of character
ajsavige at yahoo.com.au
Sun Jul 29 08:36:49 CEST 2007
--- "attn.steven.kuo at gmail.com" <attn.steven.kuo at gmail.com> wrote:
> Using itertools:
> import itertools
> s = 'ABBBCC'
> print [''.join(grp) for key, grp in itertools.groupby(s)]
> Using re:
> import re
> pat = re.compile(r'((\w)\2*)')
> print [t for t in re.findall(pat, s)]
Also nice. Especially nice that it only returns the outer parens. :-)
> By the way, your pattern seems to work in perl:
> $ perl -le '$, = " "; print split(/(?<=(.))(?!\1)/, "ABBBCC");'
> A A BBB B CC C
> Was that the type of regular expressions you were expecting?
Yes. Here's a simpler example without any backreferences:
s = re.split(r'(?<=\d)(?=\D)', '1B2D3')
That works in Perl but not in Python.
Is it that "chaining" assertions together like this is not supported in Python
Or is that the case only in the split function?
Yahoo!7 Mail has just got even bigger and better with unlimited storage on all webmail accounts.
More information about the Python-list