We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
finally I did it Because of Zeros at the beginig i waste so much time.
import java.io.;
import java.util.;
import java.util.regex.*;
public class Solution {
public static void main(String[] args) {
/* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */
Scanner sc = new Scanner(System.in);
while (sc.hasNext()) {
String str = sc.next();
System.out.println(isValidIP(str));
}
}
public static boolean isValidIP (String str) {
MyRegex myRegex = new MyRegex();
return str.matches(myRegex.strPatern);
}
}
class MyRegex {
public String strPatern = "^(0{0,2}[0-9]|0[0-9]{2}|[0-9]{1,2}|1[0-9]{2}|2[0-4][0-9]|25[0-5])\."
+ "(0{0,2}[0-9]|0[0-9]{2}|[0-9]{1,2}|1[0-9]{2}|2[0-4][0-9]|25[0-5])\."
+ "(0{0,2}[0-9]|0[0-9]{2}|[0-9]{1,2}|1[0-9]{2}|2[0-4][0-9]|25[0-5])\."
+ "(0{0,2}[0-9]|0[0-9]{2}|[0-9]{1,2}|1[0-9]{2}|2[0-4][0-9]|25[0-5])$";
public MyRegex() {}
}
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Java Regex
You are viewing a single comment's thread. Return to all comments →
finally I did it Because of Zeros at the beginig i waste so much time. import java.io.; import java.util.; import java.util.regex.*;
public class Solution {
}
class MyRegex { public String strPatern = "^(0{0,2}[0-9]|0[0-9]{2}|[0-9]{1,2}|1[0-9]{2}|2[0-4][0-9]|25[0-5])\." + "(0{0,2}[0-9]|0[0-9]{2}|[0-9]{1,2}|1[0-9]{2}|2[0-4][0-9]|25[0-5])\." + "(0{0,2}[0-9]|0[0-9]{2}|[0-9]{1,2}|1[0-9]{2}|2[0-4][0-9]|25[0-5])\." + "(0{0,2}[0-9]|0[0-9]{2}|[0-9]{1,2}|1[0-9]{2}|2[0-4][0-9]|25[0-5])$";
}