Code follows. -Walker
import re
PAT = re.compile(r'Q([^QE]+)E')
def findAllHits(s):
"""Find all non-overlapping substrings between Q and E in s."""
return PAT.findall(s)
def findShortest(l):
"""Find the list of shortest strings in l. Return l if l is empty."""
if not l:
return l
i = iter(l)
shortest = [i.next()] # Grap the first as the possible shortest string.
length = len(shortest[0])
for s in i: # Loop over the remaining strings (if any).
if len(s) < length: # New shortest string.
length = len(s)
shortest = [s]
elif len(s) == length: # A tie for shortest string.
shortest.append(s)
return shortest
if __name__ == '__main__':
# Demo.
hits = findAllHits('QUWESEADFQDFSAEDFS')
short = findShortest(hits)
print hits
print short
--
Walker Hale <walker....@gmail.com>