Valid regex not working in python
<p>I have following regex that seem to be working fine on <a href="https://regex101.com/r/zU6kG0/1" rel="nofollow">regex101</a>.
As you can see my four possible combinations are matched.  </p>

<p>Now if I want to implement this in python, I get no match at all when I try to match a line similar to the first line in regex101 string field (line with nothing after the network address). Here is the code </p>

<pre><code>import re 

l_side = 'ip prefix-list PL_FROM_7600 seq 40 permit 192.168.10.0/23'

myregex = re.compile(r'''
(ip\sprefix-list)\s
(?P&lt;prefnam&gt;\w+)\s
(?P&lt;prefseq&gt;seq\s\d+)\s
(permit|deny)\s
(?P&lt;prefnet&gt;(?:[0-9]{1,3}\.){3}[0-9]{1,3}\/[1-3]?\d)\s
((?P&lt;prefgele&gt;ge\s\d{1,2}\sle\s\d{1,2})|
(?P&lt;prefge&gt;ge\s\d{1,2})|
(?P&lt;prefle&gt;le\s\d{1,2})|)
''', re.VERBOSE)
l_match = myregex.search(l_side)

print(l_match.group('prefnam'))
print(l_match.group('prefgele'))
</code></pre>

<p>Here is the error I get</p>

<pre><code>Traceback (most recent call last):
  File "./testing.py", line 19, in &lt;module&gt;
    print(l_match.group('prefnam'))
AttributeError: 'NoneType' object has no attribute 'group'
</code></pre>

<p>if I remove the folowing from the regex</p>

<pre><code>((?P&lt;prefgele&gt;ge\s\d{1,2}\sle\s\d{1,2})|
(?P&lt;prefge&gt;ge\s\d{1,2})|
(?P&lt;prefle&gt;le\s\d{1,2})|)
</code></pre>

<p>then it works.</p>

<p>All I am trying to match here is existance of <code>gele</code> or <code>le</code> or <code>ge</code> or nothing. I have also tried the following with no luck.</p>

<pre><code>((?P&lt;prefgele&gt;ge\s\d{1,2}\sle\s\d{1,2})|
(?P&lt;prefge&gt;ge\s\d{1,2})|
(?P&lt;prefle&gt;le\s\d{1,2})|(.?))
</code></pre>

<p>Any idea on what is going on here</p>

<h1>EDIT1:</h1>

<p>After Samuel answer I have tested but it seem that all case are not matched</p>

<ol>
<li>ip prefix-list PL_FROM_7600 seq 10 permit 192.168.1.80/28</li>
<li>ip prefix-list PL_FROM_7600 seq 20 permit 192.168.1.80/28 ge 23 le 25</li>
<li>ip prefix-list PL_FROM_7600 seq 30 permit 192.168.1.80/28 ge 23  </li>
<li>ip prefix-list PL_FROM_7600 seq 40 permit 192.168.1.80/28 le 23</li>
</ol>

<p>1 and 2 are matched but not 3 and 4</p>

<p>I have amended Samuel regex to:</p>

<pre><code>(ip\sprefix-list)\s
(?P&lt;prefnam&gt;\w+)\s
(?P&lt;prefseq&gt;seq\s\d+)\s
(permit|deny)\s
(?P&lt;prefnet&gt;(?:[0-9]{1,3}\.){3}[0-9]{1,3}(?:\/[1-3])?\d)
((\s(?P&lt;prefgele&gt;ge\s\d{1,2}\sle\s\d{1,2}))|
(\s(?P&lt;prefge&gt;ge\s\d{1,2}))|
(\s(?P&lt;prefle&gt;le\s\d{1,2})))?
</code></pre>

<p>It seem to match all case now. Thanks Sam.</p>