Java Regex

  • + 0 comments
        public static void main(String[] args) {
            
            // Class with pattern
            class MyRegex {
                String pattern = "^((25[0-5]|2[0-4][0-9]|[01]?[0-9]?[0-9])\\.){3}(25[0-5]|2[0-4][0-9]|[01]?[0-9]?[0-9])$";
                }
                
            // Create a class and Scanner instances
            MyRegex myRegex = new MyRegex();
            Scanner sc = new Scanner(System.in);
    				
            // Iterate on input ip addresses
            while (sc.hasNext()) {
                String str = sc.next();
    						
                // Print true if it matches or false if it doesn't
                System.out.println(str.matches(myRegex.pattern));
            }
            // Close the Scanner
            sc.close();
        }