Pattern matches(String ,CharSequence) method in Java with Examples Last Updated : 10 May, 2019 Comments Improve Suggest changes Like Article Like Report 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: Java // 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."); } } } Output: match found for Regex. Program 2: Java // 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."); } } } 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) Comment More info A AmanSingh2210 Follow Improve Article Tags : Java Java-Functions Java 8 Java-Pattern Explore Java BasicsIntroduction to Java4 min readJava Programming Basics9 min readJava Methods7 min readAccess Modifiers in Java6 min readArrays in Java9 min readJava Strings8 min readRegular Expressions in Java7 min readOOP & InterfacesClasses and Objects in Java10 min readAccess Modifiers in Java6 min readJava Constructors10 min readJava OOP(Object Oriented Programming) Concepts10 min readJava Packages7 min readJava Interface11 min readCollectionsCollections in Java12 min readCollections Class in Java13 min readCollection Interface in Java6 min readIterator in Java5 min readJava Comparator Interface6 min readException HandlingJava Exception Handling8 min readJava Try Catch Block4 min readJava final, finally and finalize4 min readChained Exceptions in Java3 min readNull Pointer Exception in Java5 min readException Handling with Method Overriding in Java4 min readJava AdvancedJava Multithreading Tutorial3 min readSynchronization in Java10 min readFile Handling in Java4 min readJava Method References9 min readJava 8 Stream Tutorial7 min readJava Networking15+ min readJDBC Tutorial5 min readJava Memory Management4 min readGarbage Collection in Java6 min readMemory Leaks in Java3 min readPractice JavaJava Interview Questions and Answers15+ min readJava Programs - Java Programming Examples8 min readJava Exercises - Basic to Advanced Java Practice Programs with Solutions5 min readJava Quiz | Level Up Your Java Skills1 min readTop 50 Java Project Ideas For Beginners and Advanced [Update 2025]15+ min read Like