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

Fredrik Lundh fredrik at pythonware.com
Tue Dec 14 08:19:35 EST 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(m.group()) == 1:
        print m.start()

or, perhaps:

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

</F> 






More information about the Python-list mailing list