Python regex fuzzy search does not return all matches when using operator or operator

For example, when I use

regex.findall(r"(?e)(mazda2 standard){e<=1}", "mazda 2 standard")

      

as usual the answer is ['mazda 2 standard'].

But when I use

regex.findall(r"(?e)(mazda2 standard|mazda 2){e<=1}", "mazda 2 standard")

      

or

regex.findall(r"(?e)(mazda2 standard|mazda 2){e<=1}", "mazda 2 standard", overlapped=True)

      

the output does not contain "mazda 2 standard" at all. How to make the conclusion also contain "mazda 2 standard"?

+3


source to share


1 answer


See the documentation for the PyPi doc :

By default, a fuzzy match searches for the first match that meets the specified constraints. The flag ENHANCEMATCH

will cause it to try to improve the fit (i.e. reduce the number of errors) of the found match.

The flag BESTMATCH

will make him look for the best match.

You get mazda 2

with the code because this match is error free.



So, use a flag BESTMATCH

(inline modifier option (?b)

):

>>> import regex
>>> regex.findall(r"(?be)(mazda2 standard|mazda 2){e<=1}", "mazda 2 standard")
['mazda 2 standard']
>>> 

      

+1


source







All Articles