Python 的 new regex module支持模糊字符串匹配。大声歌颂(现在)。
根据文档:
The
ENHANCEMATCH
flag makes fuzzy matching attempt to improve the fit of the next match that it finds.The
BESTMATCH
flag makes fuzzy matching search for the best match instead of the next match
ENHANCEMATCH
使用 (?e)
设置标志如
regex.search("(?e)(dog){e<=1}", "cat and dog")[1]
returns "dog"
但实际上没有设置 BESTMATCH
旗帜。怎么做到的?
最佳答案
Documentation BESTMATCH
标志的功能是部分的(但正在改进)。 Poke-n-hope 显示 BESTMATCH
是使用 (?b)
设置的。
>>> import regex
>>> regex.search(r"(?e)(?:hello){e<=4}", "What did you say, oh - hello")[0]
'hat d'
>>> regex.search(r"(?b)(?:hello){e<=4}", "What did you say, oh - hello")[0]
'hello'
关于python - 如何找到最佳的模糊字符串匹配?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36818463/