Python regex's fuzzy search doesn't return all matches when using the or operator

对着背影说爱祢 提交于 2021-02-05 05:50:09

问题


For example, when I use

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

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

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 doesn't contain 'mazda 2 standard' at all. How to make the output contain 'mazda 2 standard' too?


回答1:


See PyPi regex documentation:

By default, fuzzy matching searches for the first match that meets the given constraints. The ENHANCEMATCH flag will cause it to attempt to improve the fit (i.e. reduce the number of errors) of the match that it has found.

The BESTMATCH flag will make it search for the best match instead.

You get mazda 2 with your code because this match contains no errors.

So, use the BESTMATCH flag (an inline modifier option is (?b)):

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


来源:https://stackoverflow.com/questions/43048183/python-regexs-fuzzy-search-doesnt-return-all-matches-when-using-the-or-operato

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!