python – 如何找到最佳模糊字符串匹配?
作者:互联网
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标志.怎么做的?
解决方法:
BESTMATCH标志功能上的Documentation是部分功能(但正在改进). 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,string,regex,fuzzy-search,pypi-regex 来源: https://codeday.me/bug/20190829/1758740.html