title: Python Regex search for Network Mask
body: <p>I found online some regular expressions to match an IP address. I used the one that seemed the best and then changed it to match the network mask of that IP address. This is my code:</p>
  
  <blockquote>
  <pre><code>prefix = 'None'
  while re.search(r'^(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?\.){3}(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$', prefix) is None:
      prefix = raw_input('\n\n    Enter the prefix (destination IP) &gt;  ')
  
  mask = 'None'
  while re.search(r'^((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?|0?)$', mask) is None:
      mask = raw_input('\n\n    Enter the network mask &gt;  ')
  </code></pre>
  </blockquote>
  
  <p>I tested both regex's in <a href="https://regexr.com/" rel="nofollow noreferrer">www.regexr.com</a> and both work fine, but when I run my script, the Python interpreter can't find a match in the <code>mask</code> variable, even when I insert a valid mask like <code>255.255.255.0</code>. Because of this, it is always looping over the second question.</p>
  
  <p>What is the problem here ? Should I not be using the "search" option ?</p>
  
  <p><strong>In summation:</strong> I need to verify a network mask provided by a user. It can be between <code>0.0.0.0</code> and <code>255.255.255.255</code> and it always has 4 elements separated by dots.</p>