Python 中的正则表达式匹配
Regular expression matching in Python
我想找到至少有一个错误的两个相似字符串。
我想使用 re 库中内置的 pythons。
示例
import re
re.match(r"anoother","another") #this is None indeed
它应该 return 正确并查找它是否有一个或两个拼写错误。
我找了很久的文档
但是我不知道当只有一种类型时如何使用这些知识
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
我预计 return True
re.any_regex_func(r"anyregex this is anoother line anyregex","this is another line")
如果它有不止一种类型return false
我不太确定 another
的方差。但是,也许我们可以添加一个带有负后视的中间捕获组,并传递您想要的 another
s 并使那些不想要的失败。也许,在这里我们可以定义我们的表达式类似于:
^((.+?)(another?|anoother?)(.+))$
正则表达式
如果这不是您想要的表达方式,您可以 modify/change 在 regex101.com 中表达您的表达方式。
正则表达式电路
您还可以在 jex.im:
中可视化您的表情
Python演示
# -*- coding: UTF-8 -*-
import re
string = "this is the other line\n"
expression = r'^((.+?)(another?|anoother?)(.+))$'
match = re.search(expression, string)
if match:
print("YAAAY! \"" + match.group(1) + "\" is a match ")
else:
print(' Sorry! No matches!')
输出
Sorry! No matches!
您要查找的是模糊匹配,但不幸的是,re 模块不提供此功能。
但是pypi/regex模块有,而且使用方便(可以设置pattern中一组字符的插入、删除、替换和错误的个数)。示例:
>>> import regex
>>> regex.match(r'(?:anoother){d}', 'another')
<regex.Match object; span=(0, 7), match='another', fuzzy_counts=(0, 0, 1)>
{d}
允许删除 non-capturing 组,但您可以设置允许的最大写值,例如 {d<3}
.
我想找到至少有一个错误的两个相似字符串。 我想使用 re 库中内置的 pythons。
示例
import re
re.match(r"anoother","another") #this is None indeed
它应该 return 正确并查找它是否有一个或两个拼写错误。
我找了很久的文档 但是我不知道当只有一种类型时如何使用这些知识
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
我预计 return True
re.any_regex_func(r"anyregex this is anoother line anyregex","this is another line")
如果它有不止一种类型return false
我不太确定 another
的方差。但是,也许我们可以添加一个带有负后视的中间捕获组,并传递您想要的 another
s 并使那些不想要的失败。也许,在这里我们可以定义我们的表达式类似于:
^((.+?)(another?|anoother?)(.+))$
正则表达式
如果这不是您想要的表达方式,您可以 modify/change 在 regex101.com 中表达您的表达方式。
正则表达式电路
您还可以在 jex.im:
中可视化您的表情Python演示
# -*- coding: UTF-8 -*-
import re
string = "this is the other line\n"
expression = r'^((.+?)(another?|anoother?)(.+))$'
match = re.search(expression, string)
if match:
print("YAAAY! \"" + match.group(1) + "\" is a match ")
else:
print(' Sorry! No matches!')
输出
Sorry! No matches!
您要查找的是模糊匹配,但不幸的是,re 模块不提供此功能。
但是pypi/regex模块有,而且使用方便(可以设置pattern中一组字符的插入、删除、替换和错误的个数)。示例:
>>> import regex
>>> regex.match(r'(?:anoother){d}', 'another')
<regex.Match object; span=(0, 7), match='another', fuzzy_counts=(0, 0, 1)>
{d}
允许删除 non-capturing 组,但您可以设置允许的最大写值,例如 {d<3}
.