首页 > 解决方案 > Python中的正则表达式匹配

问题描述

我想找到两个至少有一个错误的相似字符串。我想使用 re 库中内置的 python。

例子

import re

re.match(r"anoother","another") #this is None indeed

它应该返回 True 并查找它是否有一个或两个拼写错误。

我已经寻找了很长的重新文档,但我不知道当有一种类型时如何使用这些知识

a="this is the anoother line\n"
b="this is the another line\n"
c=re.search(r"{}".format(a),b) #how to write regex code here? 
#c =True  #it should return True

我期待回报True

re.any_regex_func(r"anyregex this is anoother line anyregex","this is another line")

如果它有多个类型返回 false

标签: pythonregexregex-lookaroundsregex-groupregex-greedy

解决方案


您正在寻找的是所谓的模糊匹配,但不幸的是 re 模块不提供此功能。

但是pypi/regex模块具有它并且易于使用(您可以设置模式中组允许的字符插入、删除、替换和错误的数量)。例子:

>>> import regex
>>> regex.match(r'(?:anoother){d}', 'another')
<regex.Match object; span=(0, 7), match='another', fuzzy_counts=(0, 0, 1)>

{d}允许删除非捕获组,但您可以设置允许写入的最大值,例如{d<3}.


推荐阅读