Find common substring between two strings

NorthSide picture NorthSide · Sep 10, 2013 · Viewed 95.5k times · Source

I'd like to compare 2 strings and keep the matched, splitting off where the comparison fails.

So if I have 2 strings -

string1 = apples
string2 = appleses

answer = apples

Another example, as the string could have more than one word.

string1 = apple pie available
string2 = apple pies

answer = apple pie

I'm sure there is a simple Python way of doing this but I can't work it out, any help and explanation appreciated.

Answer

RickardSjogren picture RickardSjogren · Sep 9, 2016

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 SequenceMatcher

string1 = "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 pie
print(string2[match.b: match.b + match.size])  # -> apple pie