ip adress format control java
import java.util.regex.Matcher;
import java.util.regex.Pattern;
// Java program to validate an IPv4 address
class Main
{
// an IPv4 address
private static final String INET4ADDRESS = "172.8.9.28";
private static final String IPV4_REGEX =
"^(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\." +
"(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\." +
"(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\." +
"(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$";
private static final Pattern IPv4_PATTERN = Pattern.compile(IPV4_REGEX);
public static boolean isValidInet4Address(String ip)
{
if (ip == null) {
return false;
}
Matcher matcher = IPv4_PATTERN.matcher(ip);
return matcher.matches();
}
public static void main(String[] args)
{
// Validate an IPv4 address
if (isValidInet4Address(INET4ADDRESS)) {
System.out.print("The IP address " + INET4ADDRESS + " is valid");
}
else {
System.out.print("The IP address " + INET4ADDRESS + " isn't valid");
}
}
}