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.io.*;importjava.util.*;importjava.util.regex.*;publicclassSolution{publicstaticvoidmain(String[]args){/* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */Stringrange255="(\\d{1,2}|[01][0-9][0-9]|[2][0-5][0-5])\\.";Patternipv4=Pattern.compile("^"+range255+range255+range255+range255);StringrangeHexa="(\\.{4}|((\\.{3})?)[0-9_a-fA-F]|((\\.{2})?)[0-9_a-fA-F]{2}|((\\.?)[0-9_A-Fa-f]{3})|([0-9_a-fA-F]{4})):";Patternipv6=Pattern.compile("^"+rangeHexa+rangeHexa+rangeHexa+rangeHexa+rangeHexa+rangeHexa+rangeHexa+rangeHexa);Scannersc=newScanner(System.in);Stringline="";sc.nextLine();while(sc.hasNext()){line=sc.nextLine();if(ipv4.matcher(line+".$").find()){System.out.println("IPv4");}elseif(ipv6.matcher(line+":$").find()){System.out.println("IPv6");}elseSystem.out.println("Neither");}}}
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
IP Address Validation
You are viewing a single comment's thread. Return to all comments →
JAVA