Split a string based on change of character
ajsavige at yahoo.com.au
Sun Jul 29 06:46:10 CEST 2007
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:
s = re.split(r'(?<=(.))(?!\1)', 'ABBBCC')
for e in s: print e
but was surprised when it printed:
I expected something like:
(the extra fields because of the capturing parens).
Yahoo!7 Mail has just got even bigger and better with unlimited storage on all webmail accounts.
More information about the Python-list