Pattern matcher(CharSequence) method in Java with Examples Last Updated : 14 Apr, 2023 Comments Improve Suggest changes Like Article Like Report The matcher(CharSequence) method of the Pattern class used to generate a matcher that will helpful to match the given input as parameter to method against this pattern. The Pattern.matcher() method is very helpful when we need to check a pattern against a text a single time, and the default settings of the Pattern class are appropriate. Syntax: public Matcher matcher(CharSequence input) Parameters: This method accepts a single parameter input which represents the character sequence to be matched. Return Value: This method returns a new matcher for this pattern. Below programs illustrate the matcher(CharSequence) method: Program 1: Java // Java program to demonstrate // Pattern.matcher(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"; // create a Pattern Pattern pattern = Pattern.compile(REGEX); // get a matcher object Matcher matcher = pattern.matcher(actualString); // print values if match found boolean matchfound = false; while (matcher.find()) { System.out.println("found the Regex in text:" + matcher.group() + " starting index:" + matcher.start() + " and ending index:" + matcher.end()); matchfound = true; } if (!matchfound) { System.out.println("No match found for Regex."); } } } Output:found the Regex in text:geeksforgeeks starting index:0 and ending index:13 Program 2: Java // Java program to demonstrate // Pattern.matcher(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 = "geeksforgeeks"; // create a Pattern Pattern pattern = Pattern.compile(REGEX); // get a matcher object Matcher matcher = pattern.matcher(actualString); // print values if match found boolean matchfound = false; while (matcher.find()) { System.out.println("match found"); matchfound = true; } if (!matchfound) { System.out.println("No match found"); } } } Output:No match found References: https://docs.oracle.com/javase/10/docs/api/java/util/regex/Pattern.html#matcher(java.lang.CharSequence) Comment A AmanSingh2210 Follow Improve A AmanSingh2210 Follow Improve Article Tags : Java Java-Functions Java 8 Java-Pattern Explore Java BasicsIntroduction to Java3 min readJava Programming Basics9 min readJava Methods6 min readAccess Modifiers in Java4 min readArrays in Java7 min readJava Strings8 min readRegular Expressions in Java3 min readOOP & InterfacesClasses and Objects in Java9 min readAccess Modifiers in Java4 min readJava Constructors10 min readJava OOP(Object Oriented Programming) Concepts10 min readJava Packages7 min readJava Interface7 min readCollectionsCollections in Java12 min readCollections Class in Java13 min readCollection Interface in Java4 min readIterator in Java5 min readJava Comparator Interface6 min readException HandlingJava Exception Handling6 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 Networking6 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 Examples7 min readJava Exercises - Basic to Advanced Java Practice Programs with Solutions5 min readJava Quiz1 min readJava Project Ideas For Beginners and Advanced15+ min read Like