Python Forum
Put the new line after regex pattern
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Put the new line after regex pattern
#6
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
Reply


Messages In This Thread
Put the new line after regex pattern - by stahorse - Jul-06-2023, 02:06 PM
RE: Put the new line after regex pattern - by deanhystad - Jul-10-2023, 07:29 PM

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020