Why does re.VERBOSE prevent my regex pattern from working?
<p>I want to use the following regex to get modified files from svn log, it works fine as a single line, but since it's complex, I want to use <code>re.VERBOSE</code> so that I can add comment to it, then it stopped working. What am I missing here? Thanks!</p>

<pre><code>revision='''r123456 | user | 2013-12-22 11:21:41 -0700 (Thu, 22 Dec 2013) | 1 line
Changed paths:
   A /trunk/abc/python/test/module
   A /trunk/abc/python/test/module/__init__.py
   A /trunk/abc/python/test/module/usage.py
   A /trunk/abc/python/test/module/logger.py

copied from test
'''

import re

# doesn't work
print re.search('''
            (?&lt;=Changed\spaths:\n)  
            ((\s{3}[A|M|D]\s.*\n)*)
            [(?=\n)|]       
            ''', revision, re.VERBOSE).groups()

# works
print re.search('(?&lt;=Changed\spaths:\n)((\s{3}[A|M|D]\s.*\n)*)[(?=\n)|]', revision).groups()[0]
</code></pre>

<p>The string I want to extract is: </p>

<pre><code>   A /trunk/abc/python/test/module
   A /trunk/abc/python/test/module/__init__.py
   A /trunk/abc/python/test/module/usage.py
   A /trunk/abc/python/test/module/logger.py
</code></pre>

<p>Use a raw string literal:</p>

<pre><code>re.search(r'''
            (?&lt;=Changed\spaths:\n)  
            (?:\s{3}[AMD]\s.*\n)*
            (?=\n)    
            ''', revision, re.VERBOSE)
</code></pre>

<p>See this fixed <a href="http://ideone.com/xlYIMF" rel="nofollow">Python demo</a>. </p>

<p>The main issue is that you have to pass it as a raw string literal, or use <code>\\n</code> instead of <code>\n</code>. Otherwise, <code>\n</code> (being a literal newline) is ignored inside the regex pattern, is treated as formatting whitespace (read more about that in the <a href="https://docs.python.org/2/library/re.html#re.VERBOSE" rel="nofollow">Python <code>re</code> docs</a>).</p>

<p>Also, note you corrupted the lookahead by enclosing it with <code>[...]</code> (it became a character class part) and the <code>|</code> inside character classes are treated as literal pipes (thus, here, they should be removed).</p>

python regex parsing svn