Java Regex

Sort by

recency

|

653 Discussions

|

  • + 0 comments

    import java.io.; import java.util.; import java.util.regex.*;

    public class Solution {

    public static void main(String[] args) {
      Scanner keyboard = new Scanner(System.in);
      String input;
      while(keyboard.hasNext()){
        input = keyboard.next();
        System.out.println(validate(input));
      }
      keyboard.close();
    }
    
    public static boolean validate(String input){
    
        String arr [] = input.split("[\\.]");
        if(arr.length != 4)
            return false;
    
        Pattern p = Pattern.compile("[0-9]+");
        for(String s : arr){
          if(s.length() > 3){
            return false;
          }
          Matcher m = p.matcher(s);
            if((m.matches()) == false)
                return false;
          Integer value = Integer.parseInt(s);
          if(value < 0 || value > 255)
            return false;
        }
        return true;
    }   
    

    }

  • + 0 comments

    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 pattern = "^(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\." + "(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\." + "(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\." + "(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$"; }

  • + 0 comments

    import java.io.; import java.util.;

    class Solution {

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        while(in.hasNext()) {
            System.out.println(validateIP(in.next()));
        }
    }
    
    private static boolean validateIP(String regex) {
    
        String[] regexArray = regex.split("\\.");
        if(regexArray.length != 4) {
            return false;
        }
    
        if(regexArray.length == 4) {
            String exp1 = regexArray[0];
            String exp2 = regexArray[1];
            String exp3 = regexArray[2];
            String exp4 = regexArray[3];
    
            if(exp1.length() > 3 || exp2.length() > 3 || exp3.length() > 3 || exp4. length() > 3) {
                return false;
            } 
            else if(Integer.parseInt(exp1) > 255 || Integer.parseInt(exp2) > 255 || Integer.parseInt(exp3) > 255 || Integer.parseInt(exp4) > 255) {
                return false;
            } 
            else {
                return true;
            }
        }
        return 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]?)$";

  • + 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;
         
    }