python – Find shortest matches between two strings

This is tricky to do because by default, the re module does not look at overlapping matches. Newer versions of Python have a new regex module that allows for overlapping matches.

pypi.python.org/pypi/regex

You’d want to use something like

regex.findall(pattern, string, overlapped=True)

If you’re stuck with Python 2.x or something else that doesn’t have regex, it’s still possible with some trickery. One brilliant person solved it here:

Python regex find all overlapping matches?

Once you have all possible overlapping (non-greedy, I imagine) matches, just determine which one is shortest, which should be easy.

Read more here: Source link