假设我有 string1 = "Helloworld33"
我希望能够检查 string2,以便它不包含 string1 的任何三个字符部分。
string2 可以是任意长度的任意字符集。
例如:
string1 = "Helloworld33"
string2 = "ello" (invalid)
"3hi3" (valid)
"H e l l o" (valid)
"Hw3" (valid)
"I_^&_rld_37482" (invalid)
这如何通过 python 中的正则表达式来完成?或者有没有办法做到这一点?
编辑:另外,有没有办法让它不区分大小写?
非常感谢
请您参考如下方法:
您可以使用 difflib模块:
import difflib
def solve(strs, match, n):
s = difflib.SequenceMatcher(None, strs, match)
return all(item.size < n for item in s.get_matching_blocks())
...
>>> solve('Helloworld33', 'ello', 3)
False
>>> solve('Helloworld33', '3hi3', 3)
True
>>> solve('Helloworld33', 'H e l l o', 3)
True
>>> solve('Helloworld33', 'Hw3', 3)
True
>>> solve('Helloworld33', 'I_^&_rld_37482', 3)
False
