Python_ Regular expression matching IP

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.