简体   繁体   中英

re.sub in python 2.7

Trying to replace chunks of text in html files with 'xxx' using re.sub, python 2.7. I can only get it to work with basic strings that have no spaces or new lines. This code finds nothing to replace. I've tried DOTALL, and other things, but nothing works. It just prints the whole file. I've successfully used re.search, but this won't work.

CODE:

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

IS SEARCHING (text):

<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

The fourth argument of re.sub is count . You want to set flags :

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM