Java Regex

  • + 0 comments

    import java.util.regex.*;

    class MyRegex { // Regular expression for validating the IP address String pattern = "^(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9]?[0-9])\.(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9]?[0-9])\.(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9]?[0-9])\.(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9]?[0-9])$"; }

    public class Main { public static void main(String[] args) { // Create an instance of MyRegex class MyRegex myRegex = new MyRegex();

        // Sample test cases
        System.out.println("000.12.12.034".matches(myRegex.pattern));  // true
        System.out.println("121.234.12.12".matches(myRegex.pattern));  // true
        System.out.println("23.45.12.56".matches(myRegex.pattern));    // true
        System.out.println("00.12.123.123123.123".matches(myRegex.pattern));  // false
        System.out.println("122.23".matches(myRegex.pattern));  // false
        System.out.println("Hello.IP".matches(myRegex.pattern));  // false
    }
    

    }