2016-04-24 119 views
2

Python的new regex module支持模糊字符串匹配。大声赞美(现在)。如何找到最佳的模糊字符串匹配?

每文档:

ENHANCEMATCH标志使模糊匹配为了改善下一场比赛的适应 它找到。

BESTMATCH标志使模糊匹配搜索的最佳匹配 而不是下一场比赛

ENHANCEMATCH标志使用(?e)

regex.search("(?e)(dog){e<=1}", "cat and dog")[1]返回“狗”

设置

但实际上没有设置BESTMATCH标志。它是如何完成的?

回答

2

DocumentationBESTMATCH标志的功能是部分(但改进)。 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'