IP Address Validation

  • + 3 comments

    My java solution

    public class Solution {
    
        public static void main(String[] args) {
    
            Scanner sc = new Scanner(System.in);
            int count = sc.nextInt();
            sc.nextLine();
            Pattern ip4 = Pattern.compile("^((2[0-5]\\d|[01]?\\d{0,2})\\.){3}(2[0-5]\\d|[01]?\\d{0,2})$");
            Pattern  ip6 = Pattern.compile("^([\\da-f]{1,4}:){7}[\\da-f]{1,4}$");
            while (count-- > 0){
                String line = sc.nextLine();
                Matcher m4 = ip4.matcher(line);
                Matcher m6 = ip6.matcher(line);
                if (m4.find())
                    System.out.println("IPv4");
                else if (m6.find())
                    System.out.println("IPv6");
                else 
                    System.out.println("Neither");
            }
        }
    }