Java Regex

Sort by

recency

|

678 Discussions

|

  • + 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();
        }
    
  • + 0 comments

    //Write your code here class MyRegex{ public String pattern;

    public MyRegex() {
        this.pattern = "^(\\d{1,2}\\.|[01]\\d{2}\\.|2[0-4]\\d\\.|25[0-5]\\.){3}(\\d{1,2}|[01]\\d{2}|2[0-4]\\d|25[0-5])$";
    }
    

    }

  • + 0 comments

    class MyRegex { String pattern = "^((25[0-5]|2[0-4]\d|1\d{2}|0\d{2}|\d{1,2})\.){3}" + "(25[0-5]|2[0-4]\d|1\d{2}|0\d{2}|\d{1,2})$"; }

  • + 0 comments

    class MyRegex { public static String pattern = "^((25[0-5]|2[0-4]\d|1\d\d|0?\d\d|0?\d)\.){3}" + "(25[0-5]|2[0-4]\d|1\d\d|0?\d\d|0?\d)$"; }

  • + 0 comments

    This is a really practical exercise! Writing a MyRegex class to validate IP addresses not only tests your understanding of regular expressions but also teaches you how to handle real-world data validation. cbtf turbo 247 login