[英]How to parse IP Address or Host name containing Alpha Characters?
I am trying to separate two different types of Strings, IP addresses and DNS addresses. 我试图分离两种不同类型的字符串,IP地址和DNS地址。 I am having trouble figuring out how to split it.
我无法弄清楚如何拆分它。 My initial thought is:
我最初的想法是:
String stringIP = "123.345.12.1234";
String stringDNS = "iam.adns234.address.com";
if(!Pattern.matches("^[a-zA-Z0-9.]*$",stringDNS)){
//I only want to get IP type strings here, so I am trying to separate out anything containing alpha characters. `stringDNS` should not be allowed into the if statement.
}
You can "validate" your IP with the following pattern: 您可以使用以下模式“验证”您的IP:
String stringIP = "123.345.12.1234";
String stringDNS = "iam.adns234.address.com";
// | start of input
// | | 1-4 digits
// | | | dot
// | | | | 3 occurrences
// | | | | | 1-4 digits
// | | | | | | end of input
String pattern = "^(\\d{1,4}\\.){3}\\d{1,4}$";
System.out.println(stringIP.matches(pattern));
System.out.println(stringDNS.matches(pattern));
Output 产量
true
false
Note 注意
Validation here is "lenient", insofar as it won't check the values and will fit your example with up to 4 digit items. 这里的验证是“宽松的”,因为它不会检查值并且最适合4个数字的项目。
For a more serious validation (with the down side of a less readable pattern), see dasblinkenlight 's answer. 对于更严肃的验证(具有较不易读的模式的缺点 ),请参阅dasblinkenlight的回答。
public static final String IPV4_REGEX = "\\A(25[0-5]|2[0-4]\\d|[0-1]?\\d?\\d)(\\.(25[0-5]|2[0-4]\\d|[0-1]?\\d?\\d)){3}\\z";
if(string.matches(IPV4_REGEX)
{
//....
}
Note that the IP address you posted "123.345.12.1234" is not a valid IPV4 addrss. 请注意,您发布的“123.345.12.1234”的IP地址不是有效的IPV4地址。
Your implementation would let through some strings that do not represent a digital IP address. 您的实现将允许通过一些不代表数字IP地址的字符串。 It is better to use a regex that passes through only valid numeric IP addresses.
最好使用仅通过有效数字IP地址的正则表达式。 The regex is taken from here .
正则表达式取自这里 。 It is somewhat complex, because it needs to ensure that multi-digit numbers are below 256, but it is a solid way of validating a numeric IP address:
它有点复杂,因为它需要确保多位数字低于256,但它是验证数字IP地址的可靠方法:
String IPADDRESS_PATTERN =
"^([01]?\\d\\d?|2[0-4]\\d|25[0-5])\\." +
"([01]?\\d\\d?|2[0-4]\\d|25[0-5])\\." +
"([01]?\\d\\d?|2[0-4]\\d|25[0-5])\\." +
"([01]?\\d\\d?|2[0-4]\\d|25[0-5])$";
if(Pattern.matches(IPADDRESS_PATTERN, stringIP)) {
...
}
As validating a domain String is a more complex task, if you don't mind, you can add an extra lightweight apache commons library to your project. 验证域String是一项更复杂的任务,如果您不介意,可以在项目中添加一个额外的轻量级apache commons库。
Have a look at DomainValidator.isValid(String) . 看看DomainValidator.isValid(String) 。
Please note that this method also checks for valid well-recognized top-level domain names (.com, .org, etc). 请注意,此方法还会检查有效的公认顶级域名(.com,.org等)。
For validating IP addresses have a look at InetAddressValidator.isValidInet4Address(String) . 要验证IP地址,请查看InetAddressValidator.isValidInet4Address(String) 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.