问题
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.
TheBESTMATCH
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