Pattern matches(String ,CharSequence) method in Java with Examples

The matches(String, CharSequence) method of the Pattern class in Java is used to answer whether or not the regular expression matches on the input. To do so we compile the given regular expression and attempts to match the given input against it where both regular expression and input passed as a parameter to the method. If a pattern is to be used multiple times, compiling it once and reusing it will be more efficient than invoking this method each time.

Syntax:

public static boolean matches(String regex, CharSequence input)

Parameters: This method accepts two parameters:

  • regex: This parameter represents the expression to be compiled.
  • input: The character sequence to be matched.

Return Value: This method returns a boolean value, answering whether or not the regular expression matches on the input.

Below programs illustrate the matches(String, CharSequence) method:



Program 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to demonstrate
// Pattern.matches(String, CharSequence) method
  
import java.util.regex.*;
  
public class GFG {
    public static void main(String[] args)
    {
        // create a REGEX String
        String REGEX = "(.*)(ee)(.*)?";
  
        // create the string
        // in which you want to search
        String actualString
            = "geeksforgeeks";
  
        // use matches method to check the match
        boolean matcher = Pattern.matches(REGEX, actualString);
  
        // print values if match found
        if (matcher) {
            System.out.println("match found for Regex.");
        }
        else {
            System.out.println("No match found for Regex.");
        }
    }
}

chevron_right


Output:

match found for Regex.

Program 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to demonstrate
// Pattern.matches(String, CharSequence) method
  
import java.util.regex.*;
  
public class GFG {
    public static void main(String[] args)
    {
        // create a REGEX String
        String REGEX = "(.*)(welcome)(.*)?";
  
        // create the string
        // in which you want to search
        String actualString
            = "The indian team wins worldcup";
  
        // use matches() method to check the match
        boolean matcher = Pattern.matches(REGEX, actualString);
  
        // print values if match found
        if (matcher) {
            System.out.println("match found for Regex.");
        }
        else {
            System.out.println("No match found for Regex.");
        }
    }
}

chevron_right


Output:

No match found for Regex.

Reference:
https://docs.oracle.com/javase/10/docs/api/java/util/regex/Pattern.html#matches(java.lang.String, java.lang.CharSequence)

Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



Improved By : Kirti_Mangal

Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.