Using a RegEx to match IP addresses in Python

后端 未结 13 2430
不思量自难忘°
不思量自难忘° 2020-12-01 04:58

I\'m trying to make a test for checking whether a sys.argv input matches the RegEx for an IP address...

As a simple test, I have the following...

imp         


        
13条回答
  •  攒了一身酷
    2020-12-01 05:10

    def ipcheck():
    # 1.Validate the ip adderess
    input_ip = input('Enter the ip:')
    flag = 0
    
    pattern = "^\d{1,3}.\d{1,3}.\d{1,3}.\d{1,3}$"
    match = re.match(pattern, input_ip)
    if (match):
        field = input_ip.split(".")
        for i in range(0, len(field)):
            if (int(field[i]) < 256):
                flag += 1
            else:
                flag = 0
    if (flag == 4):
        print("valid ip")
    else:
        print('No match for ip or not a valid ip')
    

提交回复
热议问题