给定文本中的通用HTML代码段,是否有任何方法可以将块1替换为块2:
<br /> Text2 <br />
<p> Text2 </p>
到目前为止,这是我使用python和正则表达式所能做到的
text = '<p>Text1</p> <br/ >Text2 <br /> <p> </p> <br/>'
pattern = "<br />(?!<p>|</p>)<br />"
matches = [ match for match in re.finditer(pattern, text) ]
#matches = [ '<p>Text1</p> <br/ >Text2 <br /> <p> </p> <br/>' ]
它匹配整个文本,但我只对一次替换(一行)感兴趣。这是一种很好的方法吗?或者您更喜欢捕获其中的内容,即“Text2”并在所需的最终文本中插入<p> </p>
块的内部
final_text = '<p>Text1</p> <p>Text2 </p> <p> </p> <br/>'
下面的例子是给你一个想法,你可以自己实现
结果:
相关问题 更多 >
编程相关推荐