How to find the best match for a fuzzy string?

Python's new regex module supports fuzzy string matching. Sing out loud (now).

In documents:

The flag ENHANCEMATCHattempts a fuzzy match to improve the match of the next match it finds.

The flag BESTMATCHsearches for fuzzy matches for the best match instead of the next match

The flag is ENHANCEMATCHset using (?e), as in

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

but the flag is not actually set BESTMATCH. How it's done?

+2
source share
1 answer

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'
+3

All Articles