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
- ABCD:EF01:2345:6789:ABCD:EF01:2345:6789
- 2001:DB8:0:0:8:800:200C:417A
- FF01:0:0:0:0:0:0:101
- 0:0:0:0:0:0:0:1
- 0:0:0:0:0:0:0:0
- 2001:DB8::8:800:200C:417A
- FF01::101
- ::1
- ::
- 0:0:0:0:0:0:13.1.68.3
- 0:0:0:0:0:FFFF:129.144.52.38
- ::13.1.68.3
- FFFF:129.144.52.38
- 2001:0DB8:0000:CD30:0000:0000:0000:0000/60
- 2001:0DB8::CD30:0:0:0:0/60
- 2001:0DB8:0:CD30::/60
NOT legal representations of IPV6
- 2001:0DB8:0:CD3/60
- 2001:0DB8::CD30/60
- 2001:0DB8::CD3/60
Strictly speaking, a section 2.3 does not describe an address representation, but a representation of address prefix (even the "full-length" prefix is not the same as an address).
That means you may safely disregard this format if you need to validate addresses.
You can use the Guava library, specifically using the
com.google.common.net.InetAddresses
class, callingisInetAddress()
.isInetAddress
public static boolean isInetAddress(String ipString)
Returns true if the supplied string is a valid IP string literal, false otherwise.
Parameters:
ipString
- String to evaluated as an IP string literalReturns:
true
if the argument is a valid IP string literalSee if this works:
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.
My idea is to split it into two part, prefix address and prefix len.
The IPAddress Java library supports parsing both IPv4 and IPv6 CIDR subnets (ie address/prefix format) in a polymorphic manner. Disclaimer: I am the project manager.
The following method is example code for validating:
Using the examples provided in the question, the output of the above method is:
As you can see, the question was incorrect about FFFF:129.144.52.38 being valid and about 2001:db8::cd30/60 and 2001:db8::cd3/60 being invalid. The first one would be valid if it were ::FFFF:129.144.52.38
I had tried below regex in java and it worked for IPV4 and IPV6