Java Regex

Sort by

recency

|

647 Discussions

|

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

    class MyRegex {

       String pat = "(\\b\\d\\b|\\b[1-9][1-9]\\b|\\b[0-1][0-9][0-9]\\b|\\b2[0-4][0-9]\\b|\\b25[0-5]\\b)";
       String pattern = "^"+pat+"\\."+pat+"\\."+pat+"\\."+pat+"$";
    

    }

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

    } public class MyRegex { public static void main(String[] args) { MyRegex regex = new MyRegex(); Scanner scanner = new Scanner(System.in);

        while (scanner.hasNext()) {
            String ip = scanner.next();
            // Validate the IP against the regex pattern
            System.out.println(ip.matches(new MyRegex().pattern));
        }
    
        scanner.close();
    }
    

    }

  • + 0 comments
    class MyRegex {
        private String patternFor256 = "([01]?[0-9][0-9]?|2[0-4][0-9]|25[0-5])";
        public String pattern = String.format("%1$s.%1$s.%1$s.%1$s", patternFor256);
    }
    
  • + 0 comments

    my solution:

    import java.io.*;
    import java.util.*;
    
    public class Solution {
    
            public static void main(String[] args) {
    
            Scanner sc = new Scanner(System.in);
            while(sc.hasNext())
            {
                String IP = sc.next();
                System.out.println(IP.matches(new MyRegex().pattern));
            }
    
        }
    }
    
    class MyRegex {
    
        String pattern = "^(([0-1]?[0-9][0-9]?|2[0-4][0-9]|25[0-5])\\.){3}(([0-1]?[0-9][0-9]?|2[0-4][0-9]|25[0-5]))$";
    }