Split python regex code into multiple lines
<p>The following code is working fine</p>

<pre><code>#!/usr/bin/python3

import re

preflist = 'ip prefix-list PL_FROM_7600 seq 255 permit 192.168.0.0/15 ge 20 le 25'
mymatch = re.search(r'(ip prefix-list) (?P&lt;prefname&gt;\w+) (?P&lt;prefseq&gt;seq \d+) (permit|deny)', preflist)

print(mymatch)

if mymatch is not None:
    print(mymatch.group('prefname'))
    print(mymatch.group('prefseq'))
</code></pre>

<p>and print the following which prove that my regex is working fine:</p>

<pre><code>[user@serv:14:14:58~/scripts]$ ./retest.py
&lt;_sre.SRE_Match object; span=(0, 42), match='ip prefix-list PL_FROM_7600 seq 255 permit'&gt;

PL_FROM_7600

seq 255
</code></pre>

<p>So now I am trying to split this into multiple lines (for <a href="https://www.python.org/dev/peps/pep-0008/" rel="nofollow">PEP8 </a> compliance of <a href="https://www.python.org/dev/peps/pep-0008/#maximum-line-length" rel="nofollow">maximum 72 characters per line</a>) using <code>re.VERBOSE</code> but keep getting errors. I have tried pretty much all combination including <code>re.compile</code> and using raw or not.</p>

<p>Here is an example that I have tried directly from the python console;</p>

<pre><code>&gt;&gt;&gt; preflist = 'ip prefix-list PL_FROM_7600 seq 255 permit 192.168.0.0/15 ge 20 le 25'

&gt;&gt;&gt; myregex = re.compile(r'''
... (ip prefix-list)
... (?P&lt;prefname&gt;\w+)
... (?P&lt;prefseq&gt;seq \d+)
... (permit|deny)
... ''', re.VERBOSE)

&gt;&gt;&gt; myregex
re.compile('\n(ip prefix-list)\n(?P&lt;prefname&gt;\\w+)\n(?P&lt;prefseq&gt;seq \\d+)\n(permit|deny)\n', re.VERBOSE)

&gt;&gt;&gt; x = myregex.search(preflist)
&gt;&gt;&gt; x
&gt;&gt;&gt; print(x)
None
</code></pre>

<p>Another example that I have tried...    </p>

<pre><code>&gt;&gt;&gt; mypattern ='''
... (ip prefix-list)
... (\s?P&lt;prefname&gt;\w+)
... (\s?P&lt;prefseq&gt;seq \d+)
... (\spermit|deny)
... '''

&gt;&gt;&gt; mypattern
'\n(ip prefix-list)\n(\\s?P&lt;prefname&gt;\\w+)\n(\\s?P&lt;prefseq&gt;seq \\d+)\n(\\spermit|deny)\n'

&gt;&gt;&gt; preflist = 'ip prefix-list PL_FROM_7600 seq 255 permit 192.168.0.0/15 ge 20 le 25'
&gt;&gt;&gt; preflist
'ip prefix-list PL_FROM_7600 seq 255 permit 192.168.0.0/15 ge 20 le 25'


&gt;&gt;&gt; mymatch = re.search(mypattern, preflist,re.VERBOSE)

&gt;&gt;&gt; mymatch
&gt;&gt;&gt; print(mymatch)
None
&gt;&gt;&gt;
</code></pre>