Regular expressions issue with python due to values with brackets
<p>I have a very large string (300 MB+), and it has some garbage data in it that I need to clean up.  I am using Python 2.7 32-bit.</p>

<p>I didn't want to use the string operation <code>replace</code> because the file the user uses is only going to grow over time, so I am trying to use <code>re.sub</code> to replace the value of <code>[linender]</code> with a new line character like <code>\n</code> or <code>os.linesep</code>.</p>

<p>It seems simple enough to do, so my pattern is:</p>

<p><code>re.sub('\[lineender]\b, os.linesep, text_value)</code></p>

<p>This results in only one value being replaced in the whole string, which is wrong.</p>

<p>Sample Data:</p>

<p><code>s = """A|B|3[lineender]E|F|2M[lineender]"""</code></p>

<p>Any ideas on how I need to modify my regex to get this working?
I basically need to replace the bracket word with a new line character.</p>

<p>You need to pass the pattern as a raw string:</p>

<pre><code>re.sub(r'\[lineender\]\b', os.linesep, text_value)
</code></pre>

<p>alternatively, you'll have to use <code>\\</code> (double backslashes):</p>

<pre><code>re.sub('\\[lineender\\]\\b', os.linesep, text_value)
</code></pre>

python regex python-2.7