RegEx: find all occurances of a single character in a string

Fredrik Lundh fredrik at
Tue Dec 14 14:19:35 CET 2004

Franz Steinhaeusler wrote:
> given a string:
> st="abcdatraataza"
>    ^   ^     ^ ^ (these should be found)
> I want to get the positions of all single 'a' characters.

for m in re.finditer("a+", st):
    if len( == 1:
        print m.start()

or, perhaps:

indexes = [m.start() for m in re.finditer("a+", st) if len( == 1]


More information about the Python-list mailing list