[Tutor] Regex across multiple lines

Kent Johnson kent37 at tds.net
Wed Apr 26 13:21:09 CEST 2006


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



More information about the Tutor mailing list