python - 如何找到最佳的模糊字符串匹配?

标签 python regex string fuzzy-search pypi-regex

Python 的 new regex module支持模糊字符串匹配。大声歌颂(现在)。

根据文档:

The ENHANCEMATCH flag makes fuzzy matching attempt to improve the fit of the next match that it finds.

The BESTMATCH flag makes fuzzy matching search for the best match instead of the next match

ENHANCEMATCH使用 (?e) 设置标志如

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

但实际上没有设置 BESTMATCH旗帜。怎么做到的?

最佳答案

Documentation 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'

关于python - 如何找到最佳的模糊字符串匹配?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36818463/

相关文章:

c# - N 个连续字符的 .NET 正则表达式

Ruby - 将 "=="十六进制值与字符串进行比较

php - 如何从核心php中的两个变量中提取常用词

python - 根据 Python 中的值在列表中查找位置?

python - PyQt:激活 QListWidget 项目

python - 谷歌云视觉批量处理大量图像使用python

python - 在系数之间添加乘号(*)

python - 从url中提取数字

python - 匹配正则表达式(python)

java - 有没有比 String.split() 更有效的方法将字符串分解为单词?