re and binary file, python strange behavior
<p>i have a strange behavior of re.search on a binary file. Here is my python screenshot :</p>

<p><a href="http://i.stack.imgur.com/OlLnw.png" rel="nofollow"><img src="http://i.stack.imgur.com/OlLnw.png" alt="enter image description here"></a></p>

<p>As you can see, i have two problems :</p>

<ul>
<li>re can't find a pattern which is indeed in the file</li>
<li>include "\x5b" in an expression results in an error</li>
</ul>

<p>Any idea?</p>

<p><code>\x5b</code> is the ASCII <code>[</code> character, the left square bracket. That's a <em>regex meta character</em> forming the start of a <code>[...]</code> character class specification and needs to be escaped if you want to match a literal <code>[</code> character:</p>

<pre><code>&gt;&gt;&gt; import re
&gt;&gt;&gt; re.search('[', '')
Traceback (most recent call last):
  File "&lt;stdin&gt;", line 1, in &lt;module&gt;
  File "/Users/mjpieters/Development/venvs/stackoverflow-2.7/lib/python2.7/re.py", line 146, in search
    return _compile(pattern, flags).search(string)
  File "/Users/mjpieters/Development/venvs/stackoverflow-2.7/lib/python2.7/re.py", line 251, in _compile
    raise error, v # invalid expression
sre_constants.error: unexpected end of regular expression
&gt;&gt;&gt; re.search('\[', '')
</code></pre>

<p>The same applies to <code>\x41</code>, that's the <code>^</code> character, which in a regex context matches the <em>start</em> of the string only, not the literal character <code>^</code>. Since you tried to match data <em>before</em> the <code>^</code> point the regex can't 
match anything, simply because that makes the anchor invalid.</p>

<p>If you are only searching for literal text matches, don't use a regex. You could just use <a href="https://docs.python.org/2/library/stdtypes.html#str.find" rel="nofollow"><code>str.find()</code></a> or <a href="https://docs.python.org/2/library/stdtypes.html#str.index" rel="nofollow"><code>str.index()</code></a> to get the index of matched text.</p>

<p>If you are using this in a larger expression and <em>generate</em> the expression from data, then use <a href="https://docs.python.org/2/library/re.html#re.escape" rel="nofollow"><code>re.escape()</code></a> to ensure all metacharacters are properly escaped first.</p>

python regex binaryfiles