Python3根据标记类型的条件替换标记
我希望文本中看起来像Python3根据标记类型的条件替换标记,python,regex,Python,Regex,我希望文本中看起来像或的所有标记分别替换为Bob Alice和Nelson Mandela。因此,基本上,根据类型是TypeA还是TypeB,我想使用Python3和regex相应地替换文本字符串中的文本 我尝试在python中执行以下操作,但不确定这是否是正确的方法: import re def my_replace(): re.sub(r'\<(.*?)\>', replace_function, data) 重新导入 def my_replace(): re.sub(
或
的所有标记分别替换为Bob Alice
和Nelson Mandela
。因此,基本上,根据类型是TypeA
还是TypeB
,我想使用Python3和regex相应地替换文本字符串中的文本
我尝试在python中执行以下操作,但不确定这是否是正确的方法:
import re
def my_replace():
re.sub(r'\<(.*?)\>', replace_function, data)
重新导入
def my_replace():
re.sub(r'\',替换函数,数据)
在上面,我尝试对
标记和我找到的每个标记执行正则表达式,我将其传递给一个名为replace\u function
的函数,以在标记之间分割文本,确定它是TypeA
还是TypeB
,并计算内容并动态返回替换标记。我甚至不确定是否可以使用re.sub
实现这一点,但任何线索都会有所帮助。多谢各位
示例:
变成Bob Alice
变成纳尔逊·曼德拉
- 很抱歉,这不是一个完整的答案,但我在电脑前睡着了,但这是与您提供的任何字符串匹配的正则表达式,
()
。检查测试你的正则表达式。很抱歉,这不是一个完整的答案,但我在电脑前睡着了,但这是正则表达式,它将匹配你提供的任何字符串,()
。检查并测试您的正则表达式。如果您将此代码的格式设置为
:
def replaceupdate(tag):
replace = ''
t = ''
i = 1
ident = ''
name = ''
typex = ''
while t != ':':
typex += tag[i]
t = tag[i]
i += 1
t = ''
while t != '|':
if tag[i] == '|':
break
ident += tag[i]
t = tag[i]
i += 1
t = ''
i += 1
while t != '>':
name += tag[i]
t = tag[i]
i += 1
replace = '<a my-inner-type="{}{}">{}</a>'.format(typex, ident, name)
return replace
def replaceupdate(标记):
替换=“”
t=''
i=1
标识=“”
名称=“”
typex=“”
而t!=':':
typex+=标记[i]
t=标签[i]
i+=1
t=''
而t!=“|”:
如果标记[i]='|':
打破
标识+=标记[i]
t=标签[i]
i+=1
t=''
i+=1
而t!='>':
名称+=标记[i]
t=标签[i]
i+=1
replace='{}'。格式(typex,ident,name)
返回替换
我知道它不使用正则表达式,它必须以其他方式拆分文本,但这是主要部分。如果您将此代码以
的形式存在,则此代码将起作用:
def replaceupdate(tag):
replace = ''
t = ''
i = 1
ident = ''
name = ''
typex = ''
while t != ':':
typex += tag[i]
t = tag[i]
i += 1
t = ''
while t != '|':
if tag[i] == '|':
break
ident += tag[i]
t = tag[i]
i += 1
t = ''
i += 1
while t != '>':
name += tag[i]
t = tag[i]
i += 1
replace = '<a my-inner-type="{}{}">{}</a>'.format(typex, ident, name)
return replace
def replaceupdate(标记):
替换=“”
t=''
i=1
标识=“”
名称=“”
typex=“”
而t!=':':
typex+=标记[i]
t=标签[i]
i+=1
t=''
而t!=“|”:
如果标记[i]='|':
打破
标识+=标记[i]
t=标签[i]
i+=1
t=''
i+=1
而t!='>':
名称+=标记[i]
t=标签[i]
i+=1
replace='{}'。格式(typex,ident,name)
返回替换
我知道它不使用正则表达式,它必须以其他方式拆分文本,但这是主要部分。使用
re.sub
,这是完全可能的,使用替换功能(旨在允许动态替换)是正确的。请参阅下面与您给出的示例一起使用的示例-可能必须根据文本中存在的其他数据(即您需要忽略的其他标记)进行修改以适合您的用例
重新导入
def replace_功能(m):
#注意:为了不修改文本(即,如果要忽略此标记),
#只需执行以下操作(返回整个原始匹配):
#返回m.group(0)
内部=m.组(1)
t、 name=内部.split(“|”)
#此处的流程类型-只有在始终遵循类型的情况下,以下才起作用
#问题中给出的模式
typename=t[4:]
#编辑:根据您的编辑,您可能需要在此处进行更多处理
#例如:
如果t.split(':')[0]=='Car':
typename='CR'
#等
返回“{}”。格式(typename,name)
def my_更换(数据):
返回re.sub(r'\',替换函数,数据)
#让我们测试一下
data='我希望文本中的所有标记看起来像或被替换为'
打印(my_替换(数据))
警告:如果此文本实际上是完整的html,则正则表达式匹配将不可靠-请使用类似beautifulsoup的html处理器。;) 这在
re.sub
中是完全可能的,并且使用替换功能(该功能旨在允许动态替换)是正确的。请参阅下面与您给出的示例一起使用的示例-可能必须根据文本中存在的其他数据(即您需要忽略的其他标记)进行修改以适合您的用例
重新导入
def replace_功能(m):
#注意:为了不修改文本(即,如果要忽略此标记),
#只需执行以下操作(返回整个原始匹配):
#返回m.group(0)
内部=m.组(1)
t、 name=内部.split(“|”)
#此处的流程类型-只有在始终遵循类型的情况下,以下才起作用
#问题中给出的模式
typename=t[4:]
#编辑:根据您的编辑,您可能需要在此处进行更多处理
#例如:
如果t.split(':')[0]=='Car':
typename='CR'
#等
返回“{}”。格式(typename,name)
def my_更换(数据):
返回re.sub(r'\',替换函数,数据)
#让我们测试一下
data='我希望文本中的所有标记看起来像或被替换为'
打印(my_替换(数据))
警告:如果此文本实际上是完整的html,则正则表达式匹配将不可靠-请使用类似beautifulsoup的html处理器。;) 试试:
import re
def get_tag(match):
base = '<a my-inner-type="{}">{}</a>'
inner_type = match.group(1).upper()
my_inner_type = '{}{}:{}'.format(inner_type[0], inner_type[-1], match.group(2))
return base.format(my_inner_type, match.group(3))
print(re.sub(r'\<(\w+):(\d+)\W([^\>]+).*', get_tag, '<Bus:1234|Bob Alice>'))
print(re.sub(r'\<(\w+):(\d+)\W([^\>]+).*', get_tag, '<Car:5678|Nelson Mandela>'))
重新导入
def get_标签(匹配):
base='{}'
内部类型=match.group(1).upper()
我的内部\u type={}{}:{}。格式(内部类型[0],内部类型[-1],match.group(2))
返回base.format(my_internal_类型,match.group(3))
打印(re.sub(r'\]+).*,获取标签“”)
打印(re.sub(r'\]+).*,获取标签“”)
试试:
import re
def get_tag(match):
base = '<a my-inner-type="{}">{}</a>'
inner_type = match.group(1).upper()
my_inner_type = '{}{}:{}'.format(inner_type[0], inner_type[-1], match.group(2))
return base.format(my_inner_type, match.group(3))
print(re.sub(r'\<(\w+):(\d+)\W([^\>]+).*', get_tag, '<Bus:1234|Bob Alice>'))
print(re.sub(r'\<(\w+):(\d+)\W([^\>]+).*', get_tag, '<Car:5678|Nelson Mandela>'))
重新导入
def get_标签(匹配):
base='{}'
内部类型=match.group(1).upper()
my_inner_type='{}{}:{}'。格式(inner_type[0]
<a my-inner-type="A:1234">Bob Alice</a> or <a my-inner-type="B:5678">Nelson Mandela</a>
<a my-inner-type="BS:1234">Bob Alice</a> or <a my-inner-type="CR:5678">Nelson Mandela</a>