Python Forum
Python re.sub text manipulation on matched contents before substituting
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Python re.sub text manipulation on matched contents before substituting
#1
Hi, I have the following code below:

import re

t = '<img data src="some-thing">'
pattern = '(<img ).*?(src=")(.*?)(">)'
u = re.sub(pattern, '\\1\\2\\3\\4', t)
print(u)
The output is <img src="some-thing">. Is there a way to do some text manipulation on group 3 so it ends up being <img src="something">? I can't think of a better approach right now. This is a just a basic example of what I am trying to do, the content I am replacing is more complex that just replacing a dash. Thanks.

Found solution with a function: https://docs.python.org/3/library/re.html#text-munging
Reply


Messages In This Thread
Python re.sub text manipulation on matched contents before substituting - by xilex - May-16-2020, 02:35 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  failing to print not matched lines from second file tester_V 14 6,110 Apr-05-2022, 11:56 AM
Last Post: codinglearner
  Text File Manipulation Queries? JustJeff 2 2,124 Apr-10-2021, 08:12 PM
Last Post: JustJeff
  print only last matched line tester_V 24 6,566 Apr-30-2020, 05:16 AM
Last Post: deanhystad
  Copy same doubled matched words kozaizsvemira 2 2,048 Oct-22-2019, 08:30 AM
Last Post: kozaizsvemira
  Python requests writes the name of the file instead of contents to web page bluethundr 1 2,168 Jun-05-2019, 09:35 PM
Last Post: Larz60+
  Creation of Dynamic HTML by substituting Database values Sandy777 1 2,139 Apr-18-2019, 07:17 AM
Last Post: buran
  Python csv compare two file, update value if two value is matched kinojom 1 2,560 Apr-17-2019, 10:36 AM
Last Post: DeaD_EyE
  including big file contents in python source Skaperen 2 2,639 Nov-07-2018, 09:39 PM
Last Post: Skaperen
  [split] Python Pillow - Photo Manipulation keegan_010 1 2,981 Oct-11-2018, 09:57 AM
Last Post: Larz60+
  Python Pillow - Photo Manipulation keegan_010 2 2,925 Oct-11-2018, 03:49 AM
Last Post: keegan_010

Forum Jump:

User Panel Messages

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