How can I find the best fuzzy string match?

被刻印的时光 ゝ 提交于 2020-01-02 07:09:14

问题


Python's new regex module supports fuzzy string matching. Sing praises aloud (now).

Per the docs:

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

The ENHANCEMATCH flag is set using (?e) as in

regex.search("(?e)(dog){e<=1}", "cat and dog")[1] returns "dog"

but there's nothing on actually setting the BESTMATCH flag. How's it done?


回答1:


Documentation on the BESTMATCH flag functionality is partial (but improving). Poke-n-hope shows that BESTMATCH is set using (?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'


来源:https://stackoverflow.com/questions/36818463/how-can-i-find-the-best-fuzzy-string-match

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!