Java Regex

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

    }