IP Address Validation

  • + 0 comments
    import re
    
    pattern_ipv4 = r'^(\d{1,2}|1\d{2}|2[0-4]\d|25[0-5])(\.(\d{1,2}|1\d{2}|2[0-4]\d|25[0-5])){3}$'
    pattern_ipv6 = r'^([\da-f]{,4})(:([\da-f]{,4})){7}$'
    
    for _ in range(int(input())):
        line = input()
        if re.match(pattern_ipv4, line):
            print('IPv4')
        elif re.match(pattern_ipv6, line):
            print('IPv6')
        else:
            print('Neither')