Python "regex"模块:模糊值



我正在使用Regex模块的"模糊匹配"功能。

我怎样才能得到"匹配"的"模糊值",这表明该模式与字符串有多大不同,就像Levenshtein中的"编辑距离"一样?

我认为我可以在Match对象中获得值,但它不在那里。官方文件也没有提及此事。

例如:

regex.match('(?:foo){e}','for')

a.captures()告诉我匹配了单词"for",但我想知道模糊值,在这种情况下应该是1

有什么方法可以做到这一点吗?

>>> import difflib
>>> matcher = difflib.SequenceMatcher(None, 'foo', 'for')
>>> sum(size for start, end, size in matcher.get_matching_blocks())
2
>>> max(map(len, ('foo', 'for'))) - _
1
>>>
>>>
>>> matcher = difflib.SequenceMatcher(None, 'foo', 'food')
>>> sum(size for start, end, size in matcher.get_matching_blocks())
3
>>> max(map(len, ('foo', 'food'))) - _
1

http://docs.python.org/2/library/difflib.html difflib.SequenceMatcher.get_matching_blockshttp://docs.python.org/2/library/difflib.html difflib.SequenceMatcher.get_opcodes

a = regex.match('(?:foo){e}','for')
a.fuzzy_counts 

返回一个元组(x,y,z),其中:

x =替换次数

y =插入数和

z =删除数

但这并不总是一个可靠的计数,即:在某些情况下,正则表达式匹配模糊度不等于真正的莱文斯坦距离

Python regex模块模糊匹配:替换计数不符合预期

最新更新