开发者

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

开发者 https://www.devze.com 2023-02-27 10:47 出处:网络
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.

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 开发者_如何学Gorepresentation, 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


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.


You can take a look at the Regular Expression Library


Bad idea. IPv6 addresses in RFC 5952 form aren't a regular language, so parsing them with a regular expression is the road to failure. Use the proper parsing function, e.g. the POSIX inet_pton function. Java should have one somewhere. Who knows, it might even be smart enough to handle IPv6 addresses with embedded IPv4 subfields according to section 5 of RFC 5952.

Seriously... don't use regular expressions for this.

0

精彩评论

暂无评论...
验证码 换一张
取 消