Java Regex

Sort by

recency

|

658 Discussions

|

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

  • + 0 comments

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

    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 ip = sc.nextLine();
    
        List<String> eachWord = new ArrayList<>(Arrays.asList(ip.split("\\.")));
    
        if(ip.charAt(ip.length()-1)=='.')
            eachWord.add("");
    
    
        boolean check = true;
    
        if(eachWord.size()==4 && eachWord.get(0).length()>0){
            for (String string : eachWord) {
                try {
                    if(string.length() > 3){
                        check = false;
                        break;
                    }
                    int num = Integer.parseInt(string);
                    if (!(num >= 0 && num <= 255)) {
                        check = false;
                        break;
                    }
                } catch (NumberFormatException e) {
                    check = false; // Handles cases where parsing fails (e.g., empty strings or non-numeric parts)
                    break;
                }
            }
        }
        else{
            check = false;
        }
    
        if(check) 
            System.out.println("true");
        else
            System.out.println("false");
       }
    }
    

    }

  • + 0 comments

    JAVA SOLUTION import java.io.; import java.util.;

    public class Solution {

    public static void regex(String ip){
        String [] ipParts = ip.split("[.]");
    
        if (ipParts.length != 4) {
            System.out.println("false");
            return;
        }
    
        for (String string : ipParts) {
            if(string.length() > 3) {
              System.out.println("false");
              return;
            }       
            else if(string.length() <= 2 && !string.matches("^[0-9][0-9]{0,3}$")){
              System.out.println("false");
              return;
            }            
    
            else if((string.length() == 3 && string.charAt(1) == '5' ) && !string.matches("^[0-2][0-5][0-5]{0,3}$")){
                System.out.println("false");
                return;
            }
    
            else if(string.length() == 3 && !string.matches("^[0-2][0-5][0-9]{0,3}$")){
    
                System.out.println("false");
                return;
            }
        }
        System.out.println("true");
    
    }
    
    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 ip = sc.nextLine();
            regex(ip);
        }        
    }
    

    }

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