如何找到最佳的模糊字符串匹配?

How can I find the best fuzzy string match?

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'