Java – how do I resolve an IP address or host name that contains alpha characters?
•
Java
I tried to separate two different types of strings, IP address and DNS address I can't figure out how to split it My initial idea was:
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. }
Solution
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";
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) { //.... }
Please note that the IP address of "123.345.12.1234" you published is not a valid IPv4 address
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码