[Tutor] Regex across multiple lines

Frank Moore francis.moore at rawflow.com
Wed Apr 26 11:15:44 CEST 2006


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.

Any ideas?

Many thanks,
Frank.



More information about the Tutor mailing list