Java Regex

Sort by

recency

|

645 Discussions

|

  • + 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]))$";
    }
    
  • + 0 comments

    class MyRegex{ String pat = "([01]?[0-9]?[0-9]?|2[0-4]?[0-9]|2[0-5]?[0-5?])"; String pattern = "^" + pat + "\." + pat + "\." + pat + "\." + pat ; }

    Here pat accepts number string only ranges between 0 to 255. Pattern make sures that IP addr should start with number 0 to 255 and "\." add point between numbers.

  • + 0 comments
    class MyRegex {
        String pattern = "\\b(?:(?:25[0-5]|2[0-4]\\d|1\\d{2}|0?\\d{1,2})\\.){3}(?:25[0-5]|2[0-4]\\d|1\\d{2}|0?\\d{1,2})\\b";
    }