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>Note that <code>\b</code> in a non-raw string literal is a backspace. If you use a word boundary <code>r'\b'</code>, it will require a word char (a letter, digit or an underscore) after <code>]</code>. In your case, I'd remove <code>\b</code> altogether:</p>

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

<p>If you want to make sure there is no word char after <code>]</code>, you may replace <code>\b</code> with <code>\B</code>, but please make sure you are using the <code>r</code> prefix to make your string literal raw.</p>

<p>See <a href="https://ideone.com/8BYN5z" rel="nofollow">Python demo</a>:</p>

<pre><code>import re, os
text_value = """A|B|3[lineender]E|F|2M[lineender]"""
print('"{}"'.format(re.sub(r'\[lineender]', os.linesep, text_value)))
</code></pre>

python regex python-2.7