在尝试查找两个字符串之间的公共子字符串时,SequenceMatcher
不会返回所有预期的公共子字符串。
s1 = '++%2F%2F+Prints+%22Hello%2C+World%22+to+the+terminal+window.%0A++++++++System.out.pr%29%3B%0A++++%7D%0A%7D%0ASample+program%0Apublic+static+voclass+id+main%28String%5B%5D+args%29+'
s2 = 'gs%29+%7B%0A++++++++%2F'
# The common substring are '+%', '%0A++++++++', '%s' and 'gs%29+'
# but 'gs%29+' is not matched.
import difflib as d
seqmatch = d.SequenceMatcher(None,s1,s2)
matches = seqmatch.get_matching_blocks()
for match in matches:
apos, bpos, matchlen = match
print(s1[apos:apos+matchlen])
输出:
+%
%0A++++++++
%2
s1
"gs%29+" 是和之间的公共子字符串s2
,但SequenceMatcher
.
我错过了什么吗?
谢谢