Dec-05-2016, 08:26 PM
"""
Create a function called
- String, representing HTML body (full or partial)
- String, representing a new TITLE that must be applied into the HTML
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
HINT: Look into the re.IGNORECASE flag for case insensitivity.
rewrite_title('<html><head><TiTLe>Foo title</title></html>', 'Bar title') ->
'<html><head><TiTLe>Bar title</title></html>'
"""
Create a function called
rewrite_title
, taking two arguments:- String, representing HTML body (full or partial)
- String, representing a new TITLE that must be applied into the HTML
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
HINT: Look into the re.IGNORECASE flag for case insensitivity.
rewrite_title('<html><head><TiTLe>Foo title</title></html>', 'Bar title') ->
'<html><head><TiTLe>Bar title</title></html>'
"""
import re def rewrite_title(*args): """ Replace the HTML title contents with the given TITLE """ htmlbody = args[0] newtitle = args[1] regex = '<title>(.+?)</title>' pattern = re.compile(regex,re.IGNORECASE) title = re.findall (pattern,htmlbody) rehtmlbody = re.sub (title, newtitle, htmlbody) print rehtmlbody hb = '<html><head><TiTLe>Foo title</title></html>' nt = 'Bar title' rewrite_title(hb,nt)what am i doing wrong,why is it not working