簡體   English   中英

python 2.7中的re.sub

[英]re.sub in python 2.7

嘗試使用python 2.7 re.sub將html文件中的文本塊替換為“ xxx”。 我只能使它與沒有空格或換行的基本字符串一起使用。 此代碼找不到要替換的內容。 我已經嘗試過DOTALL和其他方法,但是沒有任何效果。 它只是打印整個文件。 我已經成功使用了re.search,但這是行不通的。

碼:

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

re.sub的第四個參數是count 您要設置flags

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM