This function is rather inefficient, but it solves your problem:
def find_shortest_overlapping_matches(pattern, line):
pat=re.compile(pattern)
n=len(line)
ret=[]
for start in xrange(0, n):
for end in xrange(start+1, n+1):
tmp=line[start:end]
mat=pat.match(tmp)
if mat is not None:
ret.append(tmp)
break
return ret
print find_shortest_overlapping_matches("a.*d", "abcabdcd")
Output:
['abcabd', 'abd']
The ranges assume your pattern contains at least one character and does not match an empty string. Additionally, you should consider using ?
to make your patterns match non-greedily to improve performance and avoid the inner loop.