Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<pre><code>def is_valid_ip(ip): """Validates IP addresses. """ return is_valid_ipv4(ip) or is_valid_ipv6(ip) </code></pre> <p>IPv4:</p> <pre><code>def is_valid_ipv4(ip): """Validates IPv4 addresses. """ pattern = re.compile(r""" ^ (?: # Dotted variants: (?: # Decimal 1-255 (no leading 0's) [3-9]\d?|2(?:5[0-5]|[0-4]?\d)?|1\d{0,2} | 0x0*[0-9a-f]{1,2} # Hexadecimal 0x0 - 0xFF (possible leading 0's) | 0+[1-3]?[0-7]{0,2} # Octal 0 - 0377 (possible leading 0's) ) (?: # Repeat 0-3 times, separated by a dot \. (?: [3-9]\d?|2(?:5[0-5]|[0-4]?\d)?|1\d{0,2} | 0x0*[0-9a-f]{1,2} | 0+[1-3]?[0-7]{0,2} ) ){0,3} | 0x0*[0-9a-f]{1,8} # Hexadecimal notation, 0x0 - 0xffffffff | 0+[0-3]?[0-7]{0,10} # Octal notation, 0 - 037777777777 | # Decimal notation, 1-4294967295: 429496729[0-5]|42949672[0-8]\d|4294967[01]\d\d|429496[0-6]\d{3}| 42949[0-5]\d{4}|4294[0-8]\d{5}|429[0-3]\d{6}|42[0-8]\d{7}| 4[01]\d{8}|[1-3]\d{0,9}|[4-9]\d{0,8} ) $ """, re.VERBOSE | re.IGNORECASE) return pattern.match(ip) is not None </code></pre> <p>IPv6:</p> <pre><code>def is_valid_ipv6(ip): """Validates IPv6 addresses. """ pattern = re.compile(r""" ^ \s* # Leading whitespace (?!.*::.*::) # Only a single whildcard allowed (?:(?!:)|:(?=:)) # Colon iff it would be part of a wildcard (?: # Repeat 6 times: [0-9a-f]{0,4} # A group of at most four hexadecimal digits (?:(?&lt;=::)|(?&lt;!::):) # Colon unless preceeded by wildcard ){6} # (?: # Either [0-9a-f]{0,4} # Another group (?:(?&lt;=::)|(?&lt;!::):) # Colon unless preceeded by wildcard [0-9a-f]{0,4} # Last group (?: (?&lt;=::) # Colon iff preceeded by exacly one colon | (?&lt;!:) # | (?&lt;=:) (?&lt;!::) : # ) # OR | # A v4 address with NO leading zeros (?:25[0-4]|2[0-4]\d|1\d\d|[1-9]?\d) (?: \. (?:25[0-4]|2[0-4]\d|1\d\d|[1-9]?\d) ){3} ) \s* # Trailing whitespace $ """, re.VERBOSE | re.IGNORECASE | re.DOTALL) return pattern.match(ip) is not None </code></pre> <p>The IPv6 version uses "<code>(?:(?&lt;=::)|(?&lt;!::):)</code>", which could be replaced with "<code>(?(?&lt;!::):)</code>" on regex engines that support conditionals with look-arounds. (i.e. PCRE, .NET)</p> <p><strong>Edit:</strong></p> <ul> <li>Dropped the native variant.</li> <li>Expanded the regex to comply with the RFC.</li> <li>Added another regex for IPv6 addresses.</li> </ul> <p><strong>Edit2:</strong></p> <p>I found some links discussing how to parse IPv6 addresses with regex:</p> <ul> <li><a href="http://forums.dartware.com/viewtopic.php?t=452" rel="noreferrer">A Regular Expression for IPv6 Addresses</a> - InterMapper Forums</li> <li><a href="http://www.vankouteren.eu/blog/2009/05/working-ipv6-regular-expression/" rel="noreferrer">Working IPv6 regular expression</a> - Patrick’s playground blog</li> <li><a href="http://download.dartware.com/thirdparty/test-ipv6-regex.pl" rel="noreferrer">test-ipv6-regex.pl</a> - Perl script with tons of test-cases. It seems my regex fails on a few of those tests.</li> </ul> <p><strong>Edit3:</strong></p> <p>Finally managed to write a pattern that passes all tests, and that I am also happy with.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload