Given the generic HTML snippet in text, is there any way to substitute block 1 by block 2 :
<br /> Text2 <br /><p> Text2 </p>
So far this is as far as I could get using python and regex.
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/>' ]
It matches the whole text but I'm interested only in substituting in one go(one line). Is that a good approach, or perhaps you'd rather prefer capture what's inside, that is, "Text2" and insert inside of a <p> </p> block within the desired final_text?.
final_text = '<p>Text1</p> <p>Text2 </p> <p> </p> <br/>'
find (?s)<br\s*/>(.*?)<br\s*/> replace <p>\1</p>