import
java.util.regex.*;
class
IPAddressValidation {
public
static
boolean
isValidIPAddress(String ip)
{
String zeroTo255
=
"(\\d{1,2}|(0|1)\\"
+
"d{2}|2[0-4]\\d|25[0-5])"
;
String regex
= zeroTo255 +
"\\."
+ zeroTo255 +
"\\."
+ zeroTo255 +
"\\."
+ zeroTo255;
Pattern p = Pattern.compile(regex);
if
(ip ==
null
) {
return
false
;
}
Matcher m = p.matcher(ip);
return
m.matches();
}
public
static
void
main(String args[])
{
System.out.println(
"Test Case 1:"
);
String str1 =
"000.12.12.034"
;
System.out.println(
"Input: "
+ str1);
System.out.println(
"Output: "
+ isValidIPAddress(str1));
System.out.println(
"\nTest Case 2:"
);
String str2 =
"121.234.12.12"
;
System.out.println(
"Input: "
+ str2);
System.out.println(
"Output: "
+ isValidIPAddress(str2));
System.out.println(
"\nTest Case 3:"
);
String str3 =
"000.12.234.23.23"
;
System.out.println(
"Input: "
+ str3);
System.out.println(
"Output: "
+ isValidIPAddress(str3));
System.out.println(
"\nTest Case 4:"
);
String str4 =
"I.Am.not.an.ip"
;
System.out.println(
"Input: "
+ str4);
System.out.println(
"Output: "
+ isValidIPAddress(str4));
}
}