Search code examples
javaregexipv6ipv4port-number

Regular expression to validate InetSocketAddresses (ipv4/v6 + port addresses)


I am looking for tested regular expressions for both ipv4 and ipv6 InetSocketAddress (i.e., ip address + port number). I am not interested in validating hostnames.

It can be two regex (one for ipv4, one for ipv6) or one combined regex.

Does anyone have any to share?

EDIT

For ip4 format information see here, for ipv6 format information see here. Then, port number is added with ':'.

EDIT 2 To create a string representation, I will proceed like this:

byte[] tmp = { 10, 1, 0, 0 };
InetSocketAddress isa = new InetSocketAddress(
        InetAddress.getByAddress(tmp), 443);

which returns:

/10.1.0.0:443

Solution

  • Trying to use a regex on the .toString() of InetSocketAddress to do this might not be such a good idea. (see comments on question above)

    One possible alternative is to use a URL or URI to print the address in string format, which is much more standardized.


    **Edit:**

    On the other hand, if you want to torture yourself with regular expressions... ;-)

    IPv4:

          Pattern: .*/([0-9]+\.[0-9]+\.[0-9]+\.[0-9]+):([0-9]+)
    Java constant: ".*/([0-9]+\\.[0-9]+\\.[0-9]+\\.[0-9]+):([0-9]+)"
    

    Handles only dotted-quad format addresses. Does not detect invalid addresses.

    IPv6:

          Pattern: .*/([0-9a-f]+:[0-9a-f]+:[0-9a-f]+:[0-9a-f]+:[0-9a-f]+:[0-9a-f]+:[0-9a-f]+:[0-9a-f]+(%[a-zA-Z0-9]+)?):([0-9]+)
    Java constant: ".*/([0-9a-f]+:[0-9a-f]+:[0-9a-f]+:[0-9a-f]+:[0-9a-f]+:[0-9a-f]+:[0-9a-f]+:[0-9a-f]+(%[a-zA-Z0-9]+)?):([0-9]+)"
    

    Handles IPv6-addresses with all 8 16-bit sections. (note again that the only reason this works is because the Inet6Address implementation in Java seems to print the addresses in a non-standard way - probably so it can append the port number and there is no ambiguity) Does not detect invalid IPv6 addresses. Handles only lowercase hex characters. Handles zone/scope IDs (if present) with uppercase or lowercase letters and/or digits.

    I tested them with a handy applet I found.

    And for the record, I still think it's a bad idea. ;-) I can't be sure if all Java platforms will print the addresses this way.