Java Regex

Sort by

recency

|

668 Discussions

|

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

  • + 0 comments

    import java.util.Scanner;

    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(); } }

    final class MyRegex { private final 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}" + "(25[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(IP_REG_PATTERN_STRING);
    }
    

    }

  • + 0 comments

    String regex = "^(25[0-5]|2[0-4][0-9]|[0-1]?[0-9]{1,2})\.(25[0-5]|2[0-4][0-9]|[0-1]?[0-9]{1,2})\.(25[0-5]|2[0-4][0-9]|[0-1]?[0-9]{1,2})\.(25[0-5]|2[0-4][0-9]|[0-1]?[0-9]{1,2})$";