Regular Expressions in Java

Regular Expressions or Regex (in short) is an API for defining String patterns that can be used for searching, manipulating and editing a text. It is widely used to define constraint on strings such as password. Regular Expressions are provided under java.util.regex package.

The java.util.regex package primarily consists of the following three classes:

  1. util.regex.Pattern – Used for defining patterns
  2. util.regex.Matcher – Used for performing match operations on text using patterns
  3. PatternSyntaxException– Used for indicating syntax error in a regular expression pattern

java.util.regex.Pattern Class

  1. matches()­­­– It is used to check if the whole text matches a pattern. Its output is boolean.
    // A Simple Java program to demonstrate working of
    // Pattern.matches() in Java
    import java.util.regex.Pattern;
    
    class Demo
    {
        public static void main(String args[])
        {
            // Following line prints "true" because the whole
            // text "geeksforgeeks" matches pattern "geeksforge*ks"
            System.out.println (Pattern.matches("geeksforge*ks",
                                                "geeksforgeeks"));
    
            // Following line prints "false" because the whole
            // text "geeksfor" doesn't match pattern "g*geeks*"
            System.out.println (Pattern.matches("g*geeks*",
                                                "geeksfor"));
        }
    }

    Output:

    true
    false
  2. compile()– Used to create a pattern object by compiling a given string that may contain regular expressions. Input may also contains flags like Pattern.CASE_INSENSITIVE, Pattern.COMMENTS, .. etc (See this for details).
  3. split()– It is used to split a text into multiple strings based on a delimiter pattern.

java.util.regex.Matcher Class

  1. find() –It is mainly used for searching multiple occurrences of the regular expressions in the text.
  2. start() – It is used for getting the start index of a match that is being found using find() method.
  3. end() –It is used for getting the end index of a match that is being found using find() method. It returns index of character next to last matching character

Note that Pattern.matches() checks if whole text matches with a pattern or not. Other methods (demonstrated below) are mainly used to find multiple occurrences of pattern in text.



Java Programs to demonstrate workings of compile(), find(), start(), end() and split() :

  1. Java Program to demonstrate simple pattern searching

    // A Simple Java program to demonstrate working of
    // String matching in Java
    import java.util.regex.Matcher;
    import java.util.regex.Pattern;
    
    class Demo
    {
        public static void main(String args[])
        {
            // Create a pattern to be searched
            Pattern pattern = Pattern.compile("geeks");
    
            // Search above pattern in "geeksforgeeks.org"
            Matcher m = pattern.matcher("geeksforgeeks.org");
    
            // Print starting and ending indexes of the pattern
            // in text
            while (m.find())
                System.out.println("Pattern found from " + m.start() +
                                   " to " + (m.end()-1));
        }
    }
    

    Output:

    Pattern found from 0 to 4
    Pattern found from 8 to 12
  2. Java Program to demonstrate simple regular expression searching
    // A Simple Java program to demonstrate working of
    // String matching in Java
    import java.util.regex.Matcher;
    import java.util.regex.Pattern;
    
    class Demo
    {
        public static void main(String args[])
        {
            // Create a pattern to be searched
            Pattern pattern = Pattern.compile("ge*");
    
            // Search above pattern in "geeksforgeeks.org"
            Matcher m = pattern.matcher("geeksforgeeks.org");
    
            // Print starting and ending indexes of the pattern
            // in text
            while (m.find())
                System.out.println("Pattern found from " + m.start() +
                                   " to " + (m.end()-1));
        }
    }
    

    Output:

    Pattern found from 0 to 2
    Pattern found from 8 to 10
    Pattern found from 16 to 16
    
  3. Java program to demonstrate Case Insensitive Searching
    // A Simple Java program to demonstrate working of
    // String matching in Java
    import java.util.regex.Matcher;
    import java.util.regex.Pattern;
    
    class Demo
    {
        public static void main(String args[])
        {
            // Create a pattern to be searched
            Pattern pattern = Pattern.compile("ge*", Pattern.CASE_INSENSITIVE);
    
            // Search above pattern in "geeksforgeeks.org"
            Matcher m = pattern.matcher("GeeksforGeeks.org");
    
            // Print starting and ending indexes of the pattern
            // in text
            while (m.find())
                System.out.println("Pattern found from " + m.start() +
                                   " to " + (m.end()-1));
        }
    }
    

    Output:

    Pattern found from 0 to 2
    Pattern found from 8 to 10
    Pattern found from 16 to 16
    
  4. Java program to demonstrate working of split() to split a text based on a delimiter pattern
    // Java program to demonstrate working of splitting a text by a
    // given pattern
    import java.util.regex.Matcher;
    import java.util.regex.Pattern;
    
    class Demo
    {
        public static void main(String args[])
        {
            String text = "geeks1for2geeks3";
    
            // Specifies the string pattern which is to be searched
            String delimiter =  "\\d";
            Pattern pattern = Pattern.compile(delimiter,
                                            Pattern.CASE_INSENSITIVE);
    
            // Used to perform case insensitive search of the string
            String[] result = pattern.split(text);
    
    
            for (String temp: result)
                System.out.println(temp);
        }
    }
    

    Output:

    geeks
    for
    geeks
    

Important Observations/Facts:

  1. We create a pattern object by calling Pattern.compile(), there is no constructor. compile() is a static method in Pattern class.
  2. Like above, we create a Matcher object using matcher() on objects of Pattern class.
  3. Pattern.matches() is also a static method that is used to check if given text as a whole matches pattern or not.
  4. find() is used to find multiple occurrences of pattern in text.
  5. We can split a text based on a delimiter pattern using split()

Quantifiers in Java

This article is contributed by Akash Ojha. If you like GeeksforGeeks and would like to contribute, you can also write an article and mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above




Recommended Posts:



3.4 Average Difficulty : 3.4/5.0
Based on 18 vote(s)