Matcher usePattern(Pattern) method in Java with Examples Last Updated : 25 Mar, 2022 Comments Improve Suggest changes Like Article Like Report The usePattern() method of Matcher Class is used to get the pattern to be matched by this matcher. Syntax: public Matcher usePattern(Pattern newPattern) Parameters: This method takes a parameter newPattern which is the new pattern to be set. Return Value: This method returns a Matcher with the new Pattern. Exception: This method throws IllegalArgumentException if newPattern is null. Below examples illustrate the Matcher.usePattern() method: Example 1: Java // Java code to illustrate usePattern() method import java.util.regex.*; public class GFG { public static void main(String[] args) { // Get the regex to be checked String regex = "Geeks"; // Create a pattern from regex Pattern pattern = Pattern.compile(regex); // Get the String to be matched String stringToBeMatched = "GeeksForGeeks"; // Create a matcher for the input String Matcher matcher = pattern .matcher(stringToBeMatched); // Get the new Pattern String newPattern = "GFG"; // Get the Pattern using pattern method System.out.println("Old Pattern: " + matcher.pattern()); // Set the newPattern using usePattern() method matcher = matcher .usePattern( Pattern .compile(newPattern)); // Get the Pattern // using pattern method System.out.println("New Pattern: " + matcher.pattern()); } } Output:Old Pattern: Geeks New Pattern: GFG Example 2: Java // Java code to illustrate usePattern() method import java.util.regex.*; public class GFG { public static void main(String[] args) { // Get the regex to be checked String regex = "GFG"; // Create a pattern from regex Pattern pattern = Pattern.compile(regex); // Get the String to be matched String stringToBeMatched = "GFGFGFGFGFGFGFGFGFG"; // Create a matcher for the input String Matcher matcher = pattern n.matcher(stringToBeMatched); // Get the new Pattern String newPattern = "Geeks"; // Get the Pattern using pattern method System.out.println("Old Pattern: " + matcher.pattern()); // Set the newPattern using usePattern() method matcher = matcher .usePattern( Pattern .compile(newPattern)); // Get the Pattern // using pattern method System.out.println("New Pattern: " + matcher.pattern()); } } Output:Old Pattern: GFG New Pattern: Geeks Reference: Oracle Doc Comment More infoAdvertise with us Next Article Must Coding Questions - Company-wise K Kirti_Mangal Follow Improve Article Tags : Java Java - util package Java-Functions Java-Matcher Practice Tags : Java Similar Reads Interview PreparationInterview Preparation For Software DevelopersMust Coding Questions - Company-wise Must Do Coding Questions - Topic-wiseCompany-wise Practice ProblemsCompany PreparationCompetitive ProgrammingSoftware Design-PatternsCompany-wise Interview ExperienceExperienced - Interview ExperiencesInternship - Interview ExperiencesPractice @GeeksforgeeksProblem of the DayTopic-wise PracticeDifficulty Level - SchoolDifficulty Level - BasicDifficulty Level - EasyDifficulty Level - MediumDifficulty Level - HardLeaderboard !!Explore More...Data StructuresArraysLinked ListStackQueueBinary TreeBinary Search TreeHeapHashingGraphAdvance Data StructuresMatrixStringAll Data StructuresAlgorithmsAnalysis of AlgorithmsSearching AlgorithmsSorting AlgorithmsPattern SearchingGeometric AlgorithmsMathematical AlgorithmsRandomized AlgorithmsGreedy AlgorithmsDynamic ProgrammingDivide & ConquerBacktrackingBranch & BoundAll AlgorithmsProgramming LanguagesCC++JavaPythonC#Go LangSQLPHPScalaPerlKotlinWeb TechnologiesHTMLCSSJavaScriptBootstrapTailwind CSSAngularJSReactJSjQueryNodeJSPHPWeb DesignWeb BrowserFile FormatsComputer Science SubjectsOperating SystemsDBMSComputer NetworkComputer Organization & ArchitectureTOCCompiler DesignDigital Elec. & Logic DesignSoftware EngineeringEngineering MathematicsData Science & MLComplete Data Science CourseData Science TutorialMachine Learning TutorialDeep Learning TutorialNLP TutorialMachine Learning ProjectsData Analysis TutorialTutorial LibraryPython TutorialDjango TutorialPandas TutorialKivy TutorialTkinter TutorialOpenCV TutorialSelenium TutorialGATE CSGATE CS NotesGate CornerPrevious Year GATE PapersLast Minute Notes (LMNs)Important Topic For GATE CSGATE CoursePrevious Year Paper: CS exams Like