def countoverlappingdistinct(pattern, thestring):
    total = 0
start = 0
there = re.compile(pattern)
while True:
    mo = there.search(thestring, start)
if mo is None:
    return total
total += 1
start = 1 + mo.start()
Find out how many times a regex matches in a string in Python