title: String replacement: Multiline, Case-Insensitive with Special Characters
body: <p>Objective: Given a string, replace every occurrence of <code>'&lt;?xml version="1.0" encoding="utf-8"?&gt;'</code> and uppercase cousins with the empty string <code>''</code>. </p>
  
  <p>A <code>string.replace()</code> solution and/or a <code>re.sub()</code> solution would be great. A solution based on the <code>BeautifulSoup</code> module would be considered only as last resort. </p>
  
  <ol>
  <li><p>Attempt based on <code>string.replace()</code>:</p>
  
  <pre><code>s = '1:&lt;?xml version="1.0" encoding="utf-8"?&gt;\n2:&lt;?xml version="1.0" encoding="UTF-8"?&gt;'
  ## 1:&lt;?xml version="1.0" encoding="utf-8"?&gt;
  ## 2:&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  h = '&lt;?xml version="1.0" encoding="utf-8"?&gt;'
  r = s.replace(h, '')
  ## 1:
  ## 2:&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  </code></pre></li>
  </ol>
  
  <p>Problem: does not remove occurrences with upper case formatting, as in <code>UTF-8</code>.</p>
  
  <ol start="2">
  <li><p>Attempt based on <code>re.sub()</code>:</p>
  
  <pre><code>import re
  s = '1:&lt;?xml version="1.0" encoding="utf-8"?&gt;\n2:&lt;?xml version="1.0" encoding="UTF-8"?&gt;'
  ## 1:&lt;?xml version="1.0" encoding="utf-8"?&gt;
  ## 2:&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  h = '&lt;?xml version="1.0" encoding="utf-8"?&gt;'
  r = re.sub(h, '', s, flags=re.IGNORECASE | re.MULTILINE)
  ## 1:&lt;?xml version="1.0" encoding="utf-8"?&gt;
  ## 2:&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  </code></pre></li>
  </ol>
  
  <p>Problem: does not work at all. And yet, a simpler case works:</p>
  
  <pre><code>    import re
      s = '1:a\n2:A'
      ## 1:a
      ## 2:A
      h = 'a'
      r = re.sub(h, '', s, flags=re.IGNORECASE | re.MULTILINE)
      ## 1:
      ## 2:
  </code></pre>
  
  <p>I suspect the problem comes from the special characters inside the string, e.g. <code>&lt;?xml</code>, but haven't been able to find a solution. </p>
  
  <p>The <code>&lt;?xml</code> header is introduced into my code by the <code>xml</code> parser via the <code>BeautifulSoup</code> module. I haven't had much success with <code>BeautifulSoup</code>'s methods here, e.g. <code>.find_all()</code> and <code>.replace_with()</code>. I tried <code>soup.decode_contents()</code>, which worked for some cases but not others. I'm not posting examples of what I tried, because I'd rather not use the module for the particular task at hand (I have a string, I want to output a string, and do not want <code>BeautifulSoup</code> to otherwise alter the string). With apologies to the BS die-hards. ;-)</p>