title: Regex to replace apostrophese (') - Compilation failed: missing ) at offset 50
body: <p>So I am new to regular expressions and I am attempting to write one that will allow me to replace an apostrophe (<code>'</code>) with <code>\'</code>. The regex that I came up with worked with all my test cases when I tested it on <a href="http://myregextester.com" rel="nofollow">http://myregextester.com</a>, but completely error out when I implement it in my code.</p>
  
  <p>Anyways, this is what I have as of yet:</p>
  
  <pre><code>preg_replace('/((?&lt;!\\)\'+(?=\d\ds\b))|(\b(?&lt;=\w)(?&lt;!\\)\'+(?=\w+\b))/','\'',$text);
  </code></pre>
  
  <p>and it throws this error:</p>
  
  <blockquote>
    <p>A PHP Error was encountered<br>
    Severity: Warning<br>
    Message: preg_replace() [function.preg-replace]: Compilation failed: missing ) at offset 50</p>
  </blockquote>
  
  <p>I have counted like twenty times to see where the <code>)</code> is not matching up, and to my eyes there is not. Here is one of my several counts:</p>
  
  <pre><code>( ( ?&lt;!\\ )'+ ( ?=\d\ds\b ) ) | ( \b ( ?&lt;=\w ) ( ?&lt;!\\ ) '+ ( ?=\w+\b )  )
  1 2       3   4           5 6   7    8       9 10      11   12        13 14
  </code></pre>
  
  <p>Any ideas, or pointing out my glaringly obvious mistakes a newbie can't spot would be much appreciated.</p>