2016-04-24 118 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'