title: Multiline regex replace
body: <p>I want to transform a text like:</p>
  
  <pre><code>$$
  foo
  bar
  $$
  </code></pre>
  
  <p>to</p>
  
  <pre><code>&lt;% tex
  foo
  bar
  %&gt;
  </code></pre>
  
  <p>and <code>$\alpha$</code> to <code>&lt;% tex \alpha %&gt;</code>.</p>
  
  <p>For the single line replace, I did this:</p>
  
  <pre><code>re.sub(r"\$(.*)\$", r"&lt;% tex \1 %&gt;", text)
  </code></pre>
  
  <p>...and it works fine.</p>
  
  <p>Now, I added the multiline flag to catch the multiline one:</p>
  
  <pre><code>re.sub(r"(?i)\$\$(.*)\$\$", r"&lt;% tex \1 %&gt;", text)
  </code></pre>
  
  <p>...but it returns:</p>
  
  <pre><code>&lt;% tex  %&gt;
  foo
  bar
  &lt;% tex  %&gt;
  </code></pre>
  
  <p>Why? I'm sure it's something trivial, but I can't imagine what.</p>