Java Regex

Sort by

recency

|

661 Discussions

|

  • + 0 comments
    public class Solution {
    
        public static void main(String[] args) {
            Scanner scan = new Scanner(System.in);
            while (scan.hasNext())
               System.out.println(new MyRegex(scan.nextLine()).isValidIp());
            scan.close();
        }
    }
    
    class MyRegex {
        private String val;
        private static final String IP_REG_PATTERN_STRING 
            = "((25[0-5]"
            + "|2[0-4][0-9]"
            + "|[0-1]?[0-9]?[0-9])\\.){3}"
            
            + "(2[0-5][0-5]"
            + "|2[0-4][0-9]"
            + "|[0-1]?[0-9]?[0-9])";
        
        public MyRegex(String val){
            this.val = val;
        }
        
        public boolean isValidIp(){
            return this.val.matches(MyRegex.IP_REG_PATTERN_STRING);
        }
    }
    
  • + 0 comments

    class MyRegex{ String pattern ="((2[0-5][0-5]|2[0-4][0-9]|[0-1]?[0-9]?[0-9])\.){3}(2[0-5][0-5]|2[0-4][0-9]|[0-1]?[0-9]?[0-9])"; }

  • + 0 comments

    public String pattern = String.format("^%1s\.%1s$", "((\d{1,2})|((0|1)\d{2})|(2[0-4]\d|25[0-5]))");

  • + 0 comments

    Thanks for sharing valuable post! ekbet40 Login

  • + 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 reg = "([0-9]|[0-9][0-9]|[0|1][0-9][0-9]|2[0-5][0-5]|2[0-4][0-9])"; String pattern = reg+"."+reg+"."+reg+"."+reg; }