Regex

    Regex

    Java Regex is an API that allows you to define the pattern in order to search or manipulate the string. This regex will allow you to set some constraints on the string like a password or the email validation. Regex API comes with 1 interface and the 3 classes that are present in the java.util.regex package. Below is the list of the classes and the interfaces that are provided by the java.util.regex package-

    • MatchResult interface
    • Matcher class
    • Pattern class
    • PatternSyntaxException class

    Matcher class-

    This class will implement the MatchResult() interface. This class will work as a regex engine which will perform the match operation for a character sequence.

    No.

    Method

    Description

    1

    boolean matches()

    It will test whether the regular expression matches the given pattern or not.

    2

    boolean find()

    It will find the next expression that matches the pattern.

    3

    boolean find(int start)

    It will find the next expression that matches the pattern from the given start number.

    4

    String group()

    It will return the matched subsequence.

    5

    int start()

    It will return the starting index of the matched subsequence.

    6

    int end()

    It will return the ending index of the matched subsequence.

    7

    int groupCount()

    It will return the total number of the matched subsequence.

    Pattern class-

    This class will work as the compiled version of the regular expression. It will allow you to define the pattern for the regex engine.

    No.

    Method

    Description

    1

    static Pattern compile(String regex)

    This method will compile the given regex and returns the instance of the Pattern.

    2

    Matcher matcher(CharSequence input)

    This method will create a matcher that matches the given input with the pattern.

    3

    static boolean matches(String regex, CharSequence input)

    It will work as a combination of compile and matcher methods. It compiles the regular expression and matches the given input with the pattern.

    4

    String[] split(CharSequence input)

    It will split the given input string around matches of the specified pattern.

    5

    String pattern()

    It will return the regex pattern.

    Example-

    import java.util.regex.*;  
    
    public class Simple{  
        public static void main(String args[]){  
            Pattern p = Pattern.compile(".p"); 
            Matcher m = p.matcher("qu");  
            
            boolean b = m.matches();  
            boolean b2=Pattern.compile(".s").matcher("as").matches();  
            boolean b3 = Pattern.matches(".p", "qu");  
      
            System.out.println(b+" "+b2+" "+b3);  
        }
    }  

    Output-

    Example-

    import java.util.regex.*;  
        
    public class Simple{  
        public static void main(String args[]){  
           System.out.println(Pattern.matches(".s", "as"));  
           System.out.println(Pattern.matches(".s", "mk"));  
        }
    }  

    Output-

    A regex character class-

    No.

    Character Class

    Description

    1

    [abc]

    a, b, or c (simple class)

    2

    [^abc]

    Any character except a, b, or c (negation)

    3

    [a-zA-Z]

    a through z or A through Z, inclusive (range)

    4

    [a-d[m-p]]

    a through d, or m through p: [a-dm-p] (union)

    5

    [a-z&&[def]]

    d, e, or f (intersection)

    6

    [a-z&&[^bc]]

    a through z, except for b and c: [ad-z] (subtraction)

    7

    [a-z&&[^m-p]]

    a through z, and not m through p: [a-lq-z](subtraction)

    Example-

    import java.util.regex.*;  
    
    public class Simple{  
        public static void main(String args[]){  
            System.out.println(Pattern.matches("[amn]", "adjvh"));
            System.out.println(Pattern.matches("[amn]", "a"));
        }
    }  

    Output-

    Regex quantifiers-

    This will allow you to determine the number of occurrences of a character.

    Regex

    Description

    X?

    X occurs once or not at all

    X+

    X occurs once or more times

    X*

    X occurs zero or more times

    X{n}

    X occurs n times only

    X{n,}

    X occurs n or more times

    X{y,z}

    X occurs at least y times but less than z times

    Example-

    import java.util.regex.*;  
    
    public class Simple{  
        public static void main(String args[]){  
            System.out.println(Pattern.matches("[amn]?", "a"));
            System.out.println(Pattern.matches("[amn]+", "a"));
            System.out.println(Pattern.matches("[amn]*", "ammmna"));
        }
    }

    Output-

    Regex metacharacters-

    These metacharacters will work as the shortcodes.

    Regex

    Description

    .

    Any character (may or may not match terminator)

    \d

    Any digits, short of [0-9]

    \D

    Any non-digit, short for [^0-9]

    \s

    Any whitespace character, short for [\t\n\x0B\f\r]

    \S

    Any non-whitespace character, short for [^\s]

    \w

    Any word character, short for [a-zA-Z_0-9]

    \W

    Any non-word character, short for [^\w]

    \b

    A word boundary

    \B

    A non-word boundary

    Example-

    import java.util.regex.*;  
    
    public class Simple{  
        public static void main(String args[]){  
             System.out.println(Pattern.matches("\\d", "ABC"));
             System.out.println(Pattern.matches("\\D", "abc"));
             System.out.println(Pattern.matches("\\D*", "mak"));
        }
    }

    Output-

    Java Match Regex