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

亡梦爱人 提交于 2019-12-23 16:50:53

问题


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

回答1:


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.




回答2:


You can take a look at the Regular Expression Library




回答3:


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.



来源:https://stackoverflow.com/questions/5747548/regular-expression-to-validate-inetsocketaddresses-ipv4-v6-port-addresses

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!