Jul-10-2023, 07:29 PM
(This post was last modified: Jul-10-2023, 07:29 PM by deanhystad.)
You asked for a newline after the pattern, not before. Should be an easy fix.
import re text = """<img id="Picture_1">Blablablabla <img id="Picture_2"> Blablablabla<img id="Picture_1">Blablablabla """ matches = set(re.findall(r"<img.*?>", text, re.DOTALL)) for match in matches: text = text.replace(match, f"\n{match}\n") print(text)Of course you get extra blank lines if a line starts with or ends with the pattern.
Output:<img id="Picture_1">
Blablablabla
<img id="Picture_2">
Blablablabla
<img id="Picture_1">
Blablablabla