IP Address Validation

  • + 0 comments

    Python 3

    import re
    n = int(input())
    for _ in range(n):
        address = input()
        regexv4 = r"^((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9]?[0-9])\.){3}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9]?[0-9])$"
        regexv6 = r"^([a-f0-9]{1,4}:){7}([a-f0-9]{1,4})$"
        if re.match(regexv4, address):
            print("IPv4")
        elif re.match(regexv6, address):
            print("IPv6")
        else:
            print("Neither")