I am trying to find matching blocks between two strings using Python's SequenceMatcher
. The strings are "ABCDPQRUVWXYZ"
and "PQRABCDUVWXYZ"
. However when I apply get_matching_blocks()
, the string "PQR"
is not found as a matching block.
from difflib import SequenceMatcher
str1 = "ABCDPQRUVWXYZ"
str2 = "PQRABCDUVWXYZ"
matchAll = SequenceMatcher(None, str1, str2, False).get_matching_blocks()
for i in range(0, len(matchAll)):
print(str1[matchAll[i].a: matchAll[i].a + matchAll[i].size])
This might do what you want - won't find overlapping matches though (revised to include string locations in s1 and s2 of the substrings):
prints: