简体   繁体   English

正则表达式验证InetSocketAddresses(ipv4 / v6 +端口地址)

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

I am looking for tested regular expressions for both ipv4 and ipv6 InetSocketAddress (ie, ip address + port number). 我正在为ipv4和ipv6 InetSocketAddress (即IP地址+端口号)寻找经过测试的正则表达式。 I am not interested in validating hostnames. 我对验证主机名不感兴趣。

It can be two regex (one for ipv4, one for ipv6) or one combined regex. 它可以是两个正则表达式(一个用于ipv4,一个用于ipv6)或一个组合正则表达式。

Does anyone have any to share? 有没有人分享?

EDIT 编辑

For ip4 format information see here , for ipv6 format information see here . 有关ip4格式的信息,请参见此处 ,有关ipv6格式的信息,请参见此处 Then, port number is added with ':'. 然后,端口号添加':'。

EDIT 2 To create a string representation, I will proceed like this: 编辑2要创建字符串表示,我将继续这样做:

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. 试图在InetSocketAddress.toString()上使用正则表达式来做这个可能不是一个好主意。 (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 . 一种可能的替代方法是使用URLURI以字符串格式打印地址,这更加标准化


Edit: 编辑:

On the other hand, if you want to torture yourself with regular expressions... ;-) 另一方面,如果你想用正则表达式折磨自己...... ;-)

IPv4: 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: 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. 处理所有8个16位部分的IPv6地址。 (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. (再次注意,这有效的唯一原因是因为Java中的Inet6Address实现似乎以非标准方式打印地址 - 可能因此它可以附加端口号并且没有歧义)不检测无效的IPv6地址。 Handles only lowercase hex characters. 仅处理小写十六进制字符。 Handles zone/scope IDs (if present) with uppercase or lowercase letters and/or digits. 使用大写或小写字母和/或数字处​​理区域/范围ID(如果存在)。

I tested them with a handy applet I found . 我用一个方便的applet测试了它们。

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. ;-)我无法确定所有Java平台是否都会以这种方式打印地址。

您可以查看正则表达式库

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. RFC 5952表单中的IPv6地址不是常规语言,因此使用正则表达式解析它们是失败之路。 Use the proper parsing function, eg the POSIX inet_pton function. 使用正确的解析函数,例如POSIX inet_pton函数。 Java should have one somewhere. Java应该有一个地方。 Who knows, it might even be smart enough to handle IPv6 addresses with embedded IPv4 subfields according to section 5 of RFC 5952. 谁知道,根据RFC 5952的第5节,它甚至可能足够聪明,可以处理嵌入式IPv4子域的IPv6地址。

Seriously... don't use regular expressions for this. 说真的......不要为此使用正则表达式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM