Search code examples
javaregexparsingvalidationipv6

IPv6 validation


I used IPAddressUtil.isIPv6LiteralAddress (ipAddress) method to validate IPv6, but this method fails for ipv6-address/prefix-length format (format is mentioned in RFC 4291 section 2.3) of IPV6.

Could anyone know any validators which validate " ipv6-address/prefix-length " format?

Legal representations of IPV6

  1. ABCD:EF01:2345:6789:ABCD:EF01:2345:6789
  2. 2001:DB8:0:0:8:800:200C:417A
  3. FF01:0:0:0:0:0:0:101
  4. 0:0:0:0:0:0:0:1
  5. 0:0:0:0:0:0:0:0
  6. 2001:DB8::8:800:200C:417A
  7. FF01::101
  8. ::1
  9. ::
  10. 0:0:0:0:0:0:13.1.68.3
  11. 0:0:0:0:0:FFFF:129.144.52.38
  12. ::13.1.68.3
  13. FFFF:129.144.52.38
  14. 2001:0DB8:0000:CD30:0000:0000:0000:0000/60
  15. 2001:0DB8::CD30:0:0:0:0/60
  16. 2001:0DB8:0:CD30::/60

NOT legal representations of IPV6

  1. 2001:0DB8:0:CD3/60
  2. 2001:0DB8::CD30/60
  3. 2001:0DB8::CD3/60

Solution

  • See if this works:

    try {
        if (subjectString.matches(
            "(?ix)\\A(?:                                                  # Anchor address\n" +
            " (?:  # Mixed\n" +
            "  (?:[A-F0-9]{1,4}:){6}                                # Non-compressed\n" +
            " |(?=(?:[A-F0-9]{0,4}:){2,6}                           # Compressed with 2 to 6 colons\n" +
            "     (?:[0-9]{1,3}\\.){3}[0-9]{1,3}                     #    and 4 bytes\n" +
            "     \\z)                                               #    and anchored\n" +
            "  (([0-9A-F]{1,4}:){1,5}|:)((:[0-9A-F]{1,4}){1,5}:|:)  #    and at most 1 double colon\n" +
            " |::(?:[A-F0-9]{1,4}:){5}                              # Compressed with 7 colons and 5 numbers\n" +
            " )\n" +
            " (?:(?:25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])\\.){3}  # 255.255.255.\n" +
            " (?:25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])           # 255\n" +
            "|     # Standard\n" +
            " (?:[A-F0-9]{1,4}:){7}[A-F0-9]{1,4}                    # Standard\n" +
            "|     # Compressed\n" +
            " (?=(?:[A-F0-9]{0,4}:){0,7}[A-F0-9]{0,4}               # Compressed with at most 7 colons\n" +
            "    \\z)                                                #    and anchored\n" +
            " (([0-9A-F]{1,4}:){1,7}|:)((:[0-9A-F]{1,4}){1,7}|:)    #    and at most 1 double colon\n" +
            "|(?:[A-F0-9]{1,4}:){7}:|:(:[A-F0-9]{1,4}){7}           # Compressed with 8 colons\n" +
            ")/[A-F0-9]{0,4}\\z                                                    # Anchor address")) 
            {
            // String matched entirely
        } else {
            // Match attempt failed
        } 
    } catch (PatternSyntaxException ex) {
        // Syntax error in the regular expression
    }
    

    I purchased a very helpful program called RegexMagic nearly a year ago for some complicated regular expressions I planned on using.

    This was suppose to be Java, so it should compile, I assume the /60 can be between the ranges of 0000 and FFFF you can modify that last part.

    /[A-F0-9]{0,4} is what I added to the regular expression to match your example.