Find common substring between two strings Find common substring between two strings python python

Find common substring between two strings


For completeness, difflib in the standard-library provides loads of sequence-comparison utilities. For instance find_longest_match which finds the longest common substring when used on strings. Example use:

from difflib import SequenceMatcherstring1 = "apple pie available"string2 = "come have some apple pies"match = SequenceMatcher(None, string1, string2).find_longest_match(0, len(string1), 0, len(string2))print(match)  # -> Match(a=0, b=15, size=9)print(string1[match.a: match.a + match.size])  # -> apple pieprint(string2[match.b: match.b + match.size])  # -> apple pie


def common_start(sa, sb):    """ returns the longest common substring from the beginning of sa and sb """    def _iter():        for a, b in zip(sa, sb):            if a == b:                yield a            else:                return    return ''.join(_iter())
>>> common_start("apple pie available", "apple pies")'apple pie'

Or a slightly stranger way:

def stop_iter():    """An easy way to break out of a generator"""    raise StopIterationdef common_start(sa, sb):    return ''.join(a if a == b else stop_iter() for a, b in zip(sa, sb))

Which might be more readable as

def terminating(cond):    """An easy way to break out of a generator"""    if cond:        return True    raise StopIterationdef common_start(sa, sb):    return ''.join(a for a, b in zip(sa, sb) if terminating(a == b))


One might also consider os.path.commonprefix that works on characters and thus can be used for any strings.

import oscommon = os.path.commonprefix(['apple pie available', 'apple pies'])assert common == 'apple pie'

As the function name indicates, this only considers the common prefix of two strings.