IP Address Validation

  • + 0 comments

    for Python 3:

    import re
    N= int(input())
    
    for i in range(N):
        s = input()
        try:
            regex = re.findall(r"^([\da-f]{,4}\:){7}[\da-f]{,4}$", s)
            if regex:
                print("IPv6")
            else:
                regex = re.findall(r"^((25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])\.){3}(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])$", s)
                if regex:
                    print("IPv4")
                else:
                    print("Neither")
        except Exception as e:
            pass