Determine whether it is legitimate IP address

 

ipv4

Re Import 
# simple matching string is whether a given ip address, the following example it is not the IPv4 address, but the regular expression 
IF re.match (R & lt " ^ (:? [0-9] {. 1, }. 3 \) {}. 3 [0-9] {l, 3} $. " , " 272.168,1,1 " ): 
    Print " the IP vaild " 
the else : 
    Print " the IP invaild " 
# exact match given character whether the string is an IP address 
iF re.match (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]) $?? " , " 223.168.1.1 " ): 
    Print " IP vaild "
else:
    Print " the IP invaild " 
# ip address extracted from the simple long text extraction 
string_ip = " IS the this 289.22.22.22 ip? 
Result = the re.findall (R & lt " \ B (:? [0-9] {l, 3} . \) {}. 3 [0-9] l, 3} {\ B " , string_ip)
 IF Result: 
    Print Result 
the else : 
    Print " Re IP CAN Not Find " 
# accurate extraction of the IP 
Result = the re.findall (R & lt " \ B (:( ?: 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]) \ b?? " , string_ip):
 IF the Result: 
    Print the Result 
the else : 
    Print "re cannot find ip

 

IPv6 regular expression match

= string_IPv6 " 1050: 0: 0: 0:. 5: 600: 300c: 326b " 
# match meets the requirements of IPv6 format, please note that in the example case insensitive 
IF re.match (R & lt " ^ (:? [A-F0 -9] {l, 4}:)}. 7 {[A-F0-9] {l, 4} $ " , string_IPv6, re.I): 
    Print " IPv6 vaild " 
the else : 
    Print " IPv6 invaild " 
# IPv6 extract examples in a case-insensitive 
Result = the re.findall (R & lt " (<?! [:. \ W]) (:? [A-F0-9] {l, 4}:)}. 7 {[A-F0 -9] {1, 4} ([:?!. \ w]) " , string_IPv6, re.I) 
# print extraction results 
print result

 

 

reference:

https://www.cnblogs.com/brogong/p/7929298.html

Guess you like

Origin www.cnblogs.com/sea-stream/p/11210738.html