Java Regex

Sort by

recency

|

670 Discussions

|

  • + 0 comments

    finally I did it Because of Zeros at the beginig i waste so much time. import java.io.; import java.util.; import java.util.regex.*;

    public class Solution {

    public static void main(String[] args) {
        /* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */
    
    
        Scanner sc = new Scanner(System.in);
    
        while (sc.hasNext()) {
            String str = sc.next();
            System.out.println(isValidIP(str));
        }
    
    
    }
    
    public static  boolean isValidIP (String str) {
         MyRegex myRegex = new MyRegex();
    
    
        return str.matches(myRegex.strPatern);
    
    }
    

    }

    class MyRegex { public String strPatern = "^(0{0,2}[0-9]|0[0-9]{2}|[0-9]{1,2}|1[0-9]{2}|2[0-4][0-9]|25[0-5])\." + "(0{0,2}[0-9]|0[0-9]{2}|[0-9]{1,2}|1[0-9]{2}|2[0-4][0-9]|25[0-5])\." + "(0{0,2}[0-9]|0[0-9]{2}|[0-9]{1,2}|1[0-9]{2}|2[0-4][0-9]|25[0-5])\." + "(0{0,2}[0-9]|0[0-9]{2}|[0-9]{1,2}|1[0-9]{2}|2[0-4][0-9]|25[0-5])$";

    public MyRegex() {}
    

    }

  • + 1 comment

    This is a great exercise for understanding how to construct complex regular expressions! cricketbuzz mahadev

  • + 1 comment
    class MyRegex{
        String from0to199 = "[0-1]?[0-9]?[0-9]";
        String from200to249 = "2[0-4][0-9]";
        String from250to255 = "25[0-5]";
        String from0to255 = "(" + from0to199 + "|" + from200to249 + "|" + from250to255 + ")";
        String dot = "\\.";
        String pattern = from0to255 + dot + from0to255 + dot + from0to255 + dot + from0to255;
    }
    
  • + 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{
        static final String pattern = 
            "((([0-1]?[0-9]{1,2})|(2[0-4][0-9])|(25[0-5])).){3}(([0-1]?[0-9]{1,2})|(2[0-4][0-9])|(25[0-5]))";
    }
    
  • + 0 comments

    here is my code without regex in java

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

    public class Solution {

    public static void main(String[] args) throws IOException{
    BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
    

    String input = reader.readLine(); while (input != null){ System.out.println(MyRegex.checkIP (input)); input = reader.readLine(); } }

    }

    class MyRegex { public static boolean checkIP (String input) { int startNum = 0 ; for (int i=1 ; i < 5 ; i++){ int currentDot = input.indexOf(".", startNum);

      if (currentDot == -1 && i != 4){return false ;}
     int currentNum ;
     try {
        if (currentDot == -1) {currentDot = input.length();}
         currentNum = (int) Integer.parseInt(input.substring(startNum, currentDot));
    
    if (currentNum > 255) {return false;}
     if (input.substring(startNum, currentDot).length() > 3 || input.substring(startNum, currentDot).length() == 0) {return false;} 
     if (input.contains(" ")){return false;}
    }     
     catch (Exception e){
        return false ;
     }
     startNum = currentDot + 1;
     if (i == 3 && input.indexOf(".", startNum) > 0){return false ;}
    

    } return true; }
    }