Regex for repeated character?

Doug Schwarz see at sig.for.address.edu
Sat Jun 18 01:36:08 EDT 2005


In article <5J9se.136$Q75.39412 at newshog.newsread.com>,
 Leif K-Brooks <eurleif at ecritters.biz> wrote:

> How do I make a regular expression which will match the same character
> repeated one or more times, instead of matching repetitions of any
> (possibly non-same) characters like ".+" does? In other words, I want a
> pattern like this:
> 
>  >>> re.findall(".+", "foo") # not what I want
>  ['foo']
>  >>> re.findall("something", "foo") # what I want
>  ['f', 'oo']


How's this?

  >>> [x[0] for x in re.findall(r'((.)\2*)', 'abbcccddddcccbba')]
  ['a', 'bb', 'ccc', 'dddd', 'ccc', 'bb', 'a']

-- 
Doug Schwarz
dmschwarz&urgrad,rochester,edu
Make obvious changes to get real email address.



More information about the Python-list mailing list