user1802244
user1802244

Reputation: 51

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

Upvotes: 0

Views: 1517

Answers (1)

Janne Karila
Janne Karila

Reputation: 25207

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)

Upvotes: 4

Related Questions