List Info

Thread: Regex across multiple lines




Regex across multiple lines
user name
2006-04-26 09:15:44
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.

_______________________________________________
Tutor maillist  -  Tutorpython.org
http://
mail.python.org/mailman/listinfo/tutor
Regex across multiple lines
user name
2006-04-26 11:21:09
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  -  Tutorpython.org
http://
mail.python.org/mailman/listinfo/tutor
[1-2]

about | contact  Other archives ( Real Estate discussion Medical topics )