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.
public class Solution {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
while (scan.hasNext()) {
System.out.println(new MyRegex(scan.nextLine()).isValidIp());
}
scan.close();
}
}
final class MyRegex {
private final String val;
private static final String IP_REG_PATTERN_STRING =
"((25[0-5]|2[0-4][0-9]|[0-1]?[0-9]?[0-9])\.){3}" +
"(25[0-5]|2[0-4][0-9]|[0-1]?[0-9]?[0-9])";
public MyRegex(String val) {
this.val = val;
}
public boolean isValidIp() {
return this.val.matches(IP_REG_PATTERN_STRING);
}
}
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 →
import java.util.Scanner;
public class Solution { public static void main(String[] args) { Scanner scan = new Scanner(System.in); while (scan.hasNext()) { System.out.println(new MyRegex(scan.nextLine()).isValidIp()); } scan.close(); } }
final class MyRegex { private final String val; private static final String IP_REG_PATTERN_STRING = "((25[0-5]|2[0-4][0-9]|[0-1]?[0-9]?[0-9])\.){3}" + "(25[0-5]|2[0-4][0-9]|[0-1]?[0-9]?[0-9])";
}