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.
importjava.util.regex.Matcher;importjava.util.regex.Pattern;importjava.util.Scanner;classSolution{publicstaticvoidmain(String[]args){Scannerin=newScanner(System.in);while(in.hasNext()){StringIP=in.next();System.out.println(IP.matches(newMyRegex().pattern));}}}//Write your code hereclassMyRegex{privateStringzeroTo255="((0|1)\\d{2}|2[0-4]\\d|25[0-5]|\\d{1,2})";//You can simplify it by thinking in four conditions that might happen// (0|1)\d{2} catches any three digit number starting with 0 or 1.// 2[0-4]\d catches numbers between 200 and 249.// 25[0-5] catches numbers between 250 and 255.// \d{1,2} catches any one or two digit number privateStringvalidIpPattern=zeroTo255+"\\."+zeroTo255+"\\."+zeroTo255+"\\."+zeroTo255;publicStringpattern=validIpPattern;}
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 →
With simple explanation