2012-11-06 73 views
0

尝试用'xxx'替换html文件中的文本块,使用re.sub,python 2.7。我只能使用没有空格或换行符的基本字符串。此代码找不到任何替代。我试过DOTALL和其他东西,但没有任何作用。它只是打印整个文件。我已经成功地使用了re.search,但这不起作用。re.sub in python 2.7

CODE:

print re.sub(r'table\sstyle\=(.+)script', r'xxx', text, re.S) 

正在搜索(文本):

<table style="background-color: #ecddb0"> 
<tbody> 
<TR> 
<TD> 
<style type="text/css"> 
body { 
background-color: #ffffff; 
margin: 0px; 
padding: 0px 0 0 0px; 
</style> 
<script type="text/javascript 
+2

强制性链接:http://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags/1732454#1732454 - 如果你想清理那些要走的路。 – ThiefMaster

+0

@ThiefMaster说了什么!另外,'(。+?)'也许。 – Nadh

回答

4

re.sub第四个参数是count。你想设置flags

re.sub(r'table\sstyle\=(.+)script', r'xxx', text, flags=re.S) 
+0

谢谢。将尝试它。 – user1802244