python - 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
解决方案
您正在寻找的是所谓的模糊匹配,但不幸的是 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}
.
推荐阅读
- ruby - 我可以在 Ruby 中将一个局部变量命名为“$name”,但它不会成为全局变量吗?
- python - 如何在 Python 中更新来自 DynamoDB 的最新数据条目?
- r - 过滤器命令在 Shiny 中不起作用
- python - 订阅与调用属性
- nginx - NGINX/uWSGI 反向代理 502 Bad Gateway under load
- python - 如何在 UI/web 中公开 python input() 函数,从客户端(用户)获取输入并将其传递回服务器
- python - ValueError: int() 以 10 为底的无效文字:''(语句:nu+=int(j[:l])+int(j[l:]))
- c++ - 使用自定义排序从向量创建集合
- google-cloud-platform - 如果文件夹名称中有空格,如何从谷歌云存储桶下载文件?
- python - 如何从烧瓶中的 login_required 路由获取参数?