Is there a way that I can find out how many matches of a regex are in a string in Python? For example, if I have the string \"It actually happened when it acted out of
\"It actually happened when it acted out of
You can find overlapping matches by using a noncapturing subpattern:
def count_overlapping(pattern, string): return len(re.findall("(?=%s)" % pattern, string))
import re print len(re.findall(r'ab',u'ababababa'))