<p>The Python docs say:</p>

<blockquote>
  <p>re.MULTILINE: When specified, the pattern character '^' matches at the beginning of the string and at the beginning of each line (immediately following each newline)... By default, '^' matches only at the beginning of the string...</p>
</blockquote>

<p>So what's going on when I get the following unexpected result?</p>

<pre><code>&gt;&gt;&gt; import re
&gt;&gt;&gt; s = """// The quick brown fox.
... // Jumped over the lazy dog."""
&gt;&gt;&gt; re.sub('^//', '', s, re.MULTILINE)
' The quick brown fox.\n// Jumped over the lazy dog.'
</code></pre>

[{'answer_id': 42597, 'author': 'Moe', 'author_id': 3051, 'author_profile': 'https://Stackoverflow.com/users/3051', 'pm_score': 8, 'selected': True, 'text': '<p>Look at the definition of <a href="http://docs.python.org/library/re.html#re.sub" rel="noreferrer"><code>re.sub</code></a>:</p>\n\n<pre><code>re.sub(pattern, repl, string[, count, flags])\n</code></pre>\n\n<p>The 4th argument is the count, you are using <code>re.MULTILINE</code> (which is 8) as the count, not as a flag.</p>\n\n<p>Either use a named argument:</p>\n\n<pre><code>re.sub(\'^//\', \'\', s, flags=re.MULTILINE)\n</code></pre>\n\n<p>Or compile the regex first:</p>\n\n<pre><code>re.sub(re.compile(\'^//\', re.MULTILINE), \'\', s)\n</code></pre>\n'}, {'answer_id': 2517216, 'author': 'Ferran', 'author_id': 301850, 'author_profile': 'https://Stackoverflow.com/users/301850', 'pm_score': 4, 'selected': False, 'text': "<pre><code>re.sub('(?m)^//', '', s)\n</code></pre>\n"}, {'answer_id': 7248027, 'author': 'pseudosudo', 'author_id': 600012, 'author_profile': 'https://Stackoverflow.com/users/600012', 'pm_score': 3, 'selected': False, 'text': '<p>The full definition of <a href="http://docs.python.org/library/re.html#re.sub" rel="noreferrer"><code>re.sub</code></a> is:</p>\n\n<pre><code>re.sub(pattern, repl, string[, count, flags])\n</code></pre>\n\n<p>Which means that if you tell Python what the parameters are, then you can pass <code>flags</code> without passing <code>count</code>:</p>\n\n<pre><code>re.sub(\'^//\', \'\', s, flags=re.MULTILINE)\n</code></pre>\n\n<p>or, more concisely:</p>\n\n<pre><code>re.sub(\'^//\', \'\', s, flags=re.M)\n</code></pre>\n'}]
['https://Stackoverflow.com/questions/42581', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/3594/']