IP Address Validation

  • + 0 comments
    import re
    
    r4 = re.compile(r"(([01]\d{0,2}|2[0-4]?\d?|25[0-5]|[3-9]\d{0,1})\.){3}([01]\d{0,2}|2[0-4]?\d?|25[0-5]|[3-9]\d{0,1})\s*$")
    r6 = re.compile(r"^([0-9a-f]{1,4}\:){7}[0-9a-f]{1,4}\s*$")
    lines = [input() for _ in range(int(input()))]
    print(*['IPv4' if r4.search(t) else 'IPv6' if r6.search(t) else 'Neither' for t in lines], sep='\n')