IP regular match expression for IPv4
ImportRe#simply matches whether the given string is an IP address, the following example it is not the address of the IPv4, but it satisfies the regular expressionifRe.match (R"^ (?: [0-9]{1,3}\.) {3} [0-9] {1,3}$","272.168,1,1"): Print "IP vaild"Else: Print "IP Invaild"#exactly matches whether the given string is an IP addressifRe.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 "IP Invaild"#simple extraction of IP addresses from long textSTRING_IP ="Is this 289.22.22.22 IP?result = Re.findall (r"\b (?: [0-9]{1,3}\.) {3} [0-9] {1,3}\b", STRING_IP)ifResult:PrintresultElse: Print "re cannot find IP"#Exact IP Extractionresult = Re.findall (r"\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):ifResult:PrintresultElse: Print "re cannot find IP
Regular matching expressions for IPV6
string_ipv6="1050:0:0:0:5:600:300c:326b"#if the match meets the IPV6 format requirements, please note that the case is not case sensitive.ifRe.match (R"^ (?: [a-f0-9]{1,4}:) {7}[a-f0-9]{1,4}$", String_ipv6, re. I):Print "IPv6 vaild"Else: Print "IPv6 Invaild"#extract IPv6, case insensitiveresult = Re.findall (r"(? <![:.\w]) (?: [a-f0-9]{1,4}:) {7}[a-f0-9]{1,4} (?! [:. \w])", String_ipv6, re. I)#Print Extract ResultsPrintResult
Python_ Regular expression matching IP