Java Regex

Sort by

recency

|

650 Discussions

|

  • + 0 comments

    String pattern="^(([01]?([0-9][0-9]?)|2[0-4][0-9]|25[0-5]?)\.){3}([01]?([0-9][0-9]?)|2[0-4][0-9]|25[0-5]?)$";

  • + 0 comments
    import java.util.regex.Matcher;
    import java.util.regex.Pattern;
    import java.util.Scanner;
    
    class Solution{
    
        public static void main(String[] args){
            Scanner in = new Scanner(System.in);
            while(in.hasNext()){
                String IP = in.next();
                System.out.println(IP.matches(new MyRegex().pattern));
            }
    
        }
    }
    class MyRegex{
        String p = "(([0-1]?[0-9]?[0-9])|(2[0-4][0-9])|(25[0-5]))";
        //String p = "(([0-1]?([0-9]{1,2}))|(2[0-4][0-9])|(25[0-5]))";
        String pattern= p+"."+p+"."+p+"."+p;
         
    }
    
  • + 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
    }
    

    }

  • + 0 comments

    String pattern = "^(([01]?([0-9][0-9]?)|2[0-4][0-9]|25[0-5]?)\.){3}([01]?([0-9][0-9]?)|2[0-4][0-9]|25[0-5]?)$";

  • + 1 comment

    class MyRegex {

       String pat = "(\\b\\d\\b|\\b[1-9][1-9]\\b|\\b[0-1][0-9][0-9]\\b|\\b2[0-4][0-9]\\b|\\b25[0-5]\\b)";
       String pattern = "^"+pat+"\\."+pat+"\\."+pat+"\\."+pat+"$";
    

    }