regex - 模糊正则表达式

标签 regex string fuzzy-search fuzzy-comparison tre-library

在我的工作中,我使用近似字符串匹配算法(例如 Damerau–Levenshtein 距离)来使我的代码不易出现拼写错误,并取得了很好的结果。

现在我需要将字符串与简单的正则表达式进行匹配,例如 TV Schedule for\d\d​​ (Jan|Feb|Mar|...)。这意味着字符串 TV Schedule for 10 Jan 应返回 0,而 T Schedule for 10. Jan 应返回 2。

这可以通过生成正则表达式中的所有字符串(在本例中为 100x12)并找到最佳匹配来完成,但这并不实用。

您有什么想法可以有效地做到这一点吗?

最佳答案

我找到了TRE library ,它似乎能够精确地进行正则表达式的模糊匹配。示例:http://hackerboss.com/approximate-regex-matching-in-python/ 但它只支持插入、删除和替换。没有转置。但我想这没问题。

我在以下文件上尝试了附带的 agrep 工具和正则表达式:

TV Schedule for 10Jan
TVSchedule for Jan 10
T Schedule for 10 Jan 2010
TV Schedule for 10 March
Tv plan for March

得到了

$ agrep -s -E 100 '^TV Schedule for \d\d (Jan|Feb|Mar)$' filename
1:TV Schedule for 10Jan
8:TVSchedule for Jan 10
7:T Schedule for 10 Jan 2010
3:TV Schedule for 10 March
15:Tv plan for March

非常感谢您的所有建议。

关于regex - 模糊正则表达式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2351630/

相关文章:

python - 正则表达式提取多行散列注释

regex - 正则表达式中是否 [_\s ^"] mean underscore and whitespace but not "(quote)?

swift - 如何获取字符串中第一行的范围?

search - 模糊搜索 + 倒排索引

在模糊@username 上查找用户匹配的算法

javascript - 需要用户输入的特定部分

python - 如何使用 python 在字符串中查找和计算表情符号?

java - 用分隔符分割字符串

string - bquote : How to include an expression saved as a string object?

r - 使用 R 中的模糊/近似字符串匹配合并两个数据框