Python Forum
Thread Rating:
  • 1 Vote(s) - 1 Average
  • 1
  • 2
  • 3
  • 4
  • 5
rewrite_title
#11
how do i group the title tags in regular expression....
I need the same same title tags as per the requirement

If regular expressions are grouped something like this; how do i just replace part 2 and keep the rest of the regular expression as is...

r'(<title>)(.+?)(</title>)'
  \1          \2      \3

My answer should be like this...
blah<title>Bar title</title>second title<title>Bar title</title>blah

The function must:
  1. Return a new string, where the content found between <title> and
  </title> (*case insensitive*) are replaced with the function's TITLE argument.
  All other text should remain unchanged, including the <title> opening and
  closing tags. All occurances of <title> tags should be rewritten, if there is
  more than 1.
  2. The function documentation should read:
    Replace the HTML title contents with the given TITLE

def rewrite_title(hb,nt):
    """ Replace the HTML title contents with the given TITLE """
    rhb = re.sub(r'<title>(.+?)</title>', nt,0 re.IGNORECASE)
    print rhb
hb = 'blah<title>{title}</title>second title<title>{newtitle}</title>blah'
nt = 'Bar title'
print rewrite_title(hb,nt)
Reply


Messages In This Thread
rewrite_title - by roadrage - Dec-05-2016, 08:26 PM
RE: rewrite_title - by nilamo - Dec-05-2016, 08:30 PM
RE: rewrite_title - by micseydel - Dec-05-2016, 08:32 PM
RE: rewrite_title - by roadrage - Dec-05-2016, 09:30 PM
RE: rewrite_title - by nilamo - Dec-05-2016, 10:44 PM
RE: rewrite_title - by micseydel - Dec-06-2016, 08:42 PM
RE: rewrite_title - by roadrage - Dec-07-2016, 06:32 PM
RE: rewrite_title - by nilamo - Dec-07-2016, 06:50 PM
RE: rewrite_title - by roadrage - Dec-07-2016, 07:31 PM
RE: rewrite_title - by Ofnuts - Dec-08-2016, 10:16 PM
RE: rewrite_title - by roadrage - Dec-21-2016, 04:21 PM
RE: rewrite_title - by snippsat - Dec-21-2016, 05:14 PM
RE: rewrite_title - by roadrage - Dec-21-2016, 06:47 PM

Forum Jump:

User Panel Messages

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