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

时间:2021-05-10 19:23:43

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

Python的新regex模块支持模糊字符串匹配。大声歌颂(现在)。

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

增强标记是使用in (?e)设置的

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

regex.search(“e(?)(狗){ e < = 1 }”、“猫狗”)[1]返回“狗”

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

但实际上并没有设置最佳匹配标志。它是如何做的呢?

1 个解决方案

#1


3  

Documentation on the BESTMATCH flag functionality is partial (but improving). Poke-n-hope shows that BESTMATCH is set using (?b).

关于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'

#1


3  

Documentation on the BESTMATCH flag functionality is partial (but improving). Poke-n-hope shows that BESTMATCH is set using (?b).

关于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'