Re: Split a string based on change of character

2007-07-29 Thread [EMAIL PROTECTED]
On Jul 28, 11:36 pm, Andrew Savige <[EMAIL PROTECTED]> wrote: (snipped) > > 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 Pyt

Re: Split a string based on change of character

2007-07-28 Thread Andrew Savige
--- "[EMAIL PROTECTED]" <[EMAIL PROTECTED]> wrote: > Using itertools: > > import itertools > > s = 'ABBBCC' > print [''.join(grp) for key, grp in itertools.groupby(s)] Nice. > Using re: > > import re > > pat = re.compile(r'((\w)\2*)') > print [t[0] for t in re.findall(pat, s)] Also nice. Esp

Re: Split a string based on change of character

2007-07-28 Thread [EMAIL PROTECTED]
On Jul 28, 9:46 pm, Andrew Savige <[EMAIL PROTECTED]> wrote: > Python beginner here. > > For a string 'ABBBCC', I want to produce a list ['A', 'BBB', 'CC']. > That is, break the string into pieces based on change of character. > What's the best way to do this in Python? > > Using Python 2.5.1, I tr

Split a string based on change of character

2007-07-28 Thread Andrew Savige
Python beginner here. For a string 'ABBBCC', I want to produce a list ['A', 'BBB', 'CC']. That is, break the string into pieces based on change of character. What's the best way to do this in Python? Using Python 2.5.1, I tried: import re s = re.split(r'(?<=(.))(?!\1)', 'ABBBCC') for e in s: pri