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

P at draigBrady.com P at draigBrady.com
Tue Dec 14 12:33:34 EST 2004


P at draigBrady.com wrote:
> import re
> s='abcdatraataza'
> r=re.compile('(?<!a)a(?!a)')
> a_list=[ match.start() for match in re2.finditer(s) ]
> 

Oops, tested this time:
----------------------------

import re
 

def index_letters(s,l):
     regexp='(?<!%s)%s(?!%s)' % (l,l,l)
     r=re.compile(regexp)
     return [ match.start() for match in r.finditer(s) ]
 

print index_letters('abcdatraataza','a')


-- 
Pádraig Brady - http://www.pixelbeat.org
--



More information about the Python-list mailing list