Why does re.VERBOSE prevent my regex pattern from working?

I want to use the following regular expression to retrieve modified files from an svn log, it works fine as a single line, but since it is complex, I want to use re.VERBOSEit to add a comment to it, and then it stops at work. What am I missing here? Thank!

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('''
            (?<=Changed\spaths:\n)  
            ((\s{3}[A|M|D]\s.*\n)*)
            [(?=\n)|]       
            ''', revision, re.VERBOSE).groups()

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

The line I want to extract is:

   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
+4
source share
1 answer

Use a string literal:

re.search(r'''
            (?<=Changed\spaths:\n)  
            (?:\s{3}[AMD]\s.*\n)*
            (?=\n)    
            ''', revision, re.VERBOSE)

See the corrected Python demo .

, \\n \n. \n ( ) , ( Python re docs).

, , lookahead, [...] ( ), | ( , ).

+3

Source: https://habr.com/ru/post/1657685/


All Articles