Frank Moore wrote:
> Hi,
>
> Can anyone tell me how to do a regex substitution across multiple lines
> in HTML?
> I can search for the piece of HTML I want to substitute, for instance:
>
> <title>
> This is my title
> </title>
>
> using
>
> html_text = file('some.html', 'r').read()
> search_string = '<title>.*</title>'
> p = re.compile(search_string, re.DOTALL)
>
> But when I try and use:
>
> replace_string = '<title>Another title</title>'
> html_text = re.sub(search_string, replace_string, html_text)
>
> it doesn't work.
>
> However, if the HTML is all on one line:
>
> <title>This is my title</title>
>
> it works fine.
Use your compiled regex for the sub(), so it will have the DOTALL flag set:
html_text = p.sub(replace_string, html_text)
Kent
_______________________________________________
Tutor maillist - [email protected]
http://mail.python.org/mailman/listinfo/tutor