Method signature
find_longest_match(alo, ahi, blo, bhi)
Parameters
alo: Search start position in sequence1
ahi: Search stop position in sequence1
blo: Search start position in sequence2
bhi: Search stop position in sequence2
Return value
Returns a difflib.Match object. The attribute,
difflib.Match.a - contains the starting position of the common block in the first sequence.
difflib.Match.b - contains the starting position of the common block in the second sequence.
difflib.Match.size - contains the length of the common block.
Overview
Given two sequences that are to be compared, the method find_longest_match() of the SequenceMatcher class in Python, finds the longest matching block that is present in both the sequences. The method returns an object that contains the start positions of the common largest block in the sequence1 and sequence2, along with the length.
Example
# Example Python program that finds the longest
# matching block between two sequences
import difflib
# Create an SequenceMatcher object
seqMatch = difflib.SequenceMatcher()
# Set the sequences to be compared
seq1 = "Hello World"
seq2 = "hi Hello WSpace"
seqMatch.set_seqs(seq1, seq2)
# Find the longest matching block
matched = seqMatch.find_longest_match()
print(matched)
print(type(matched))
print("Start position of the matching block in Seq1:")
print(matched.a)
print("Start position of the matching block in Seq2:")
print(matched.b)
print("Length of the matching block in Seq2:")
print(matched.size)
print("The longest matching block:")
print(seq1[matched.a:matched.size])Output
Match(a=0, b=3, size=7)
<class 'difflib.Match'>
Start position of the matching block in Seq1:
0
Start position of the matching block in Seq2:
3
Length of the matching block in Seq2:
7
The longest matching block:
Hello W