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

Steven Bethard steven.bethard at gmail.com
Tue Dec 14 18:26:54 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.
> (Without another 'a' neighbour)

You could also try negative lookahead/lookbehind assertions:

 >>> st="abcdatraataza"
 >>> for m in re.finditer("(?<!a)a(?!a)", st):
...     print m.start()
...
0
4
10
12

Steve



More information about the Python-list mailing list