具有8个或更多零的八位字节的IP地址是否为有效的IP地址? [英] Is an IP address with an octet given by 4 or more zeros a valid IP address?

查看:878
本文介绍了具有8个或更多零的八位字节的IP地址是否为有效的IP地址?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此处验证IP地址的最高投票答案这是一个正则表达式,因此自动不利)如果在一个八位字节中为其指定一个包含4个或更多零的IP地址,则返回false。

The top voted answer for validating IP addresses here (which is a regex, so automatically unfavourable) returns false if you give it an IP address with 4 or more zeros in a single octet.

请注意,它通过3或更少,在我看来,由于任意不一致自动指示代码气味(毕竟它不是基数8所以没有什么特别的基数10)约3。然而,令人惊讶的是,apache commons实用程序 InetAddressValidator 也有这种行为(至少它只使用正则表达式进行拆分!)。

Note that it passes with 3 or less, which in my opinion, automatically indicates code smell due to arbitrary inconsistency (after all it's not base 8 and so there is nothing special about 3 in base 10). Nevertheless, surprisingly the apache commons utility InetAddressValidator also has this behaviour (at least it only uses regex for the splitting!).

观察(使用Scala REPL):

Observe (using Scala REPL):

scala> new InetAddressValidator().isValidInet4Address("000.0.0.0")
res0: Boolean = true

scala> new InetAddressValidator().isValidInet4Address("0000.0.0.0")
res1: Boolean = false

现在,此答案将返回true,因为它使用的是 parseInt 循环,它没有任意区分零个数。

Now this answer would return true, as it's using parseInt in a loop which makes no arbitrary distinction between numbers of zeros.

所以它有效吗?如果它是有效的,那么这表示Apache Commons库和所述正则表达式中的错误,如果它无效,那么请提供证明它的内容的链接。

So is it valid? If it is valid then this indicates a bug in Apache Commons library and the said regex, and if it is NOT valid then please provide a link to something that proves it.

推荐答案

是的,它是有效的。

除了< a href =http://en.wikipedia.org/wiki/Dot-decimal_notation#IPv4_address =nofollow>有关IPv4地址的维基百科有关点十进制表示法的文章:


Internet协议版本4(IPv4)地址由32位,
组成,可分为4个八位字节,每个8位。这四个八位字节
是用十进制数字写的,范围从0到255,并且
连接成一个字符串,在
数字之间有完整的分隔符。

An Internet Protocol Version 4 (IPv4) address consists of 32 bits, which may be divided into four octets, 8 bits each. These four octets are written in decimal numbers, ranging from 0 to 255, and are concatenated as a character string with full stop delimiters between number.

有效的十进制数可能也可能不以前导0开头,并且前导0的数量不应有内在限制。维基页面确实澄清了前导0是有效的,但没有明确提到前导零数量的任何限制。

A valid decimal number may or may not start with leading 0's and there should be no intrinsic restriction to the number of leading 0's. The wiki page does indeed clarify that leading 0's are valid, but does not explicitly mention any limit on the number of leading zeros.

这篇关于具有8个或更多零的八位字节的IP地址是否为有效的IP地址?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆