如何检查字符串是 IPv4 / IPv6 还是域名? (Java)
How can I check if a String is IPv4 / IPv6 or domain name? (Java)
如何检查字符串是否匹配 IPv4、IPv6 地址或域名,如 www.google.com?
我为 IPv4 找到了这个:
Validating IPv4 string in Java
同样的系统适用于 IPv6,但我如何检查域名?
编辑:
澄清一下:我想检查一个字符串是否是有效的服务器地址。有效仅表示格式正确。连接将在程序的不同点进行测试。
您可以使用正则表达式来完成。您可以创建一个模式,其中前 3 个字符是 'w',然后是一个点,然后是例如 20 个字母数字字符等等。之后检查给定的字符串是否与定义的模式匹配。
您提供的link已经使用了正则表达式。
在 java.
中使用正则表达式结帐 this tutorial
Apache 有 domain name validation:
public void testValidDomains() {
assertTrue("apache.org should validate", validator.isValid("apache.org"));
assertTrue("www.google.com should validate", validator.isValid("www.google.com"));
assertTrue("test-domain.com should validate", validator.isValid("test-domain.com"));
assertTrue("test---domain.com should validate", validator.isValid("test---domain.com"));
assertTrue("test-d-o-m-ain.com should validate", validator.isValid("test-d-o-m-ain.com"));
assertTrue("two-letter domain label should validate", validator.isValid("as.uk"));
assertTrue("case-insensitive ApAchE.Org should validate", validator.isValid("ApAchE.Org"));
assertTrue("single-character domain label should validate", validator.isValid("z.com"));
assertTrue("i.have.an-example.domain.name should validate", validator.isValid("i.have.an-example.domain.name"));
}
public void testInvalidDomains() {
assertFalse("bare TLD .org shouldn't validate", validator.isValid(".org"));
assertFalse("domain name with spaces shouldn't validate", validator.isValid(" apache.org "));
assertFalse("domain name containing spaces shouldn't validate", validator.isValid("apa che.org"));
assertFalse("domain name starting with dash shouldn't validate", validator.isValid("-testdomain.name"));
assertFalse("domain name ending with dash shouldn't validate", validator.isValid("testdomain-.name"));
assertFalse("domain name starting with multiple dashes shouldn't validate", validator.isValid("---c.com"));
assertFalse("domain name ending with multiple dashes shouldn't validate", validator.isValid("c--.com"));
assertFalse("domain name with invalid TLD shouldn't validate", validator.isValid("apache.rog"));
assertFalse("URL shouldn't validate", validator.isValid("http://www.apache.org"));
assertFalse("Empty string shouldn't validate as domain name", validator.isValid(" "));
assertFalse("Null shouldn't validate as domain name", validator.isValid(null));
}
希望对您有所帮助。
不客气。极端情况太多了,最终连接测试失败的可能性仍然很大。您最终还会得到很多假阴性验证。
只需将连接测试移动到更靠近此 domain/ip 进入系统的位置。
虽然确实有很多极端情况,但这对于 The IPAddress Java library 来说很简单。我建议不要使用正则表达式,有太多的变化。免责声明:我是IPAddress库的项目经理。
示例代码如下:
check("1.2.3.4");
check("1.2.a.4");
check("::1");
check("[::1]");
check("1.2.?.4");
static void check(String hostStr) {
HostName host = new HostName(hostStr);
try {
host.validate();
if(host.isAddress()) {
IPAddress addr = host.asAddress();
System.out.println(addr.getIPVersion() + " address: " + addr);
} else {
System.out.println("host name: " + host);
}
} catch(HostNameException e) {
System.out.println(e.getMessage());
}
}
输出:
IPv4 address: 1.2.3.4
host name: 1.2.a.4
IPv6 address: ::1
IPv6 address: ::1
1.2.?.4 Host error: invalid character at index 4
如何检查字符串是否匹配 IPv4、IPv6 地址或域名,如 www.google.com?
我为 IPv4 找到了这个: Validating IPv4 string in Java 同样的系统适用于 IPv6,但我如何检查域名?
编辑: 澄清一下:我想检查一个字符串是否是有效的服务器地址。有效仅表示格式正确。连接将在程序的不同点进行测试。
您可以使用正则表达式来完成。您可以创建一个模式,其中前 3 个字符是 'w',然后是一个点,然后是例如 20 个字母数字字符等等。之后检查给定的字符串是否与定义的模式匹配。
您提供的link已经使用了正则表达式。
在 java.
中使用正则表达式结帐 this tutorialApache 有 domain name validation:
public void testValidDomains() {
assertTrue("apache.org should validate", validator.isValid("apache.org"));
assertTrue("www.google.com should validate", validator.isValid("www.google.com"));
assertTrue("test-domain.com should validate", validator.isValid("test-domain.com"));
assertTrue("test---domain.com should validate", validator.isValid("test---domain.com"));
assertTrue("test-d-o-m-ain.com should validate", validator.isValid("test-d-o-m-ain.com"));
assertTrue("two-letter domain label should validate", validator.isValid("as.uk"));
assertTrue("case-insensitive ApAchE.Org should validate", validator.isValid("ApAchE.Org"));
assertTrue("single-character domain label should validate", validator.isValid("z.com"));
assertTrue("i.have.an-example.domain.name should validate", validator.isValid("i.have.an-example.domain.name"));
}
public void testInvalidDomains() {
assertFalse("bare TLD .org shouldn't validate", validator.isValid(".org"));
assertFalse("domain name with spaces shouldn't validate", validator.isValid(" apache.org "));
assertFalse("domain name containing spaces shouldn't validate", validator.isValid("apa che.org"));
assertFalse("domain name starting with dash shouldn't validate", validator.isValid("-testdomain.name"));
assertFalse("domain name ending with dash shouldn't validate", validator.isValid("testdomain-.name"));
assertFalse("domain name starting with multiple dashes shouldn't validate", validator.isValid("---c.com"));
assertFalse("domain name ending with multiple dashes shouldn't validate", validator.isValid("c--.com"));
assertFalse("domain name with invalid TLD shouldn't validate", validator.isValid("apache.rog"));
assertFalse("URL shouldn't validate", validator.isValid("http://www.apache.org"));
assertFalse("Empty string shouldn't validate as domain name", validator.isValid(" "));
assertFalse("Null shouldn't validate as domain name", validator.isValid(null));
}
希望对您有所帮助。
不客气。极端情况太多了,最终连接测试失败的可能性仍然很大。您最终还会得到很多假阴性验证。
只需将连接测试移动到更靠近此 domain/ip 进入系统的位置。
虽然确实有很多极端情况,但这对于 The IPAddress Java library 来说很简单。我建议不要使用正则表达式,有太多的变化。免责声明:我是IPAddress库的项目经理。
示例代码如下:
check("1.2.3.4");
check("1.2.a.4");
check("::1");
check("[::1]");
check("1.2.?.4");
static void check(String hostStr) {
HostName host = new HostName(hostStr);
try {
host.validate();
if(host.isAddress()) {
IPAddress addr = host.asAddress();
System.out.println(addr.getIPVersion() + " address: " + addr);
} else {
System.out.println("host name: " + host);
}
} catch(HostNameException e) {
System.out.println(e.getMessage());
}
}
输出:
IPv4 address: 1.2.3.4
host name: 1.2.a.4
IPv6 address: ::1
IPv6 address: ::1
1.2.?.4 Host error: invalid character at index 4