我有一个txt文件,其中包含许多字符串,如下所示:
<tag1><style="1">"Lorem ipsum dolor...</style>"Lorem Ipsum dolor"</tag1><tag1>"Other Lorem ipsum Dolor"</tag1><tag1>"Lorem ipsum DOLOR"</tag1>
我要做的是复制regex模式字符串找到的每个字符串,并将其副本粘贴到每个匹配项的下面,然后将复制字符串中的特定名称更改为“tagen1”,因此结果如下所示:
<tag1><style="1">"Lorem ipsum dolor...</style>"Lorem Ipsum dolor"</tag1><tagen1><style="1">"Lorem ipsum dolor...</style>"Lorem Ipsum dolor"</tagen1>
<tag1>"Other Lorem ipsum Dolor"</tag1><tagen1>"Other Lorem ipsum Dolor"</tagen1><tag1>"Lorem ipsum DOLOR"</tag1><tagen1>"Lorem ipsum DOLOR"</tag1>
等等
我真的不知道如何从那里开始:
pattern = re.compile(r'<tag1>(.+?)<\/tag1>')
with open(f1) as f:
f1 = pattern.findall(f.read())
copy = list(zip(f1))
#here I'd like to perform copying from "copy" list to the file.
如何解决这个问题?你知道吗
使用正则表达式来完成这项任务可能不是最好的主意,但是如果我们必须部分地这样做,我们将从一个通用表达式开始来捕获标记,然后我们将编写问题的其余部分的脚本,如果我们愿意的话。你知道吗
应该有某种库/包可以帮助我们实现这一点。你知道吗
要捕获标签
Demo
测试
正则表达式电路
jex.im可视化正则表达式:
正则表达式
如果不需要此表达式并且您希望修改它,请访问regex101.com上的此链接。你知道吗
相关问题 更多 >
编程相关推荐