Matcher usePattern(Pattern) method in Java with Examples
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 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()); } } |
Old Pattern: Geeks New Pattern: GFG
Example 2:
// 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 = patter 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()); } } |
Old Pattern: GFG New Pattern: Geeks
Reference: Oracle Doc
Recommended Posts:
- Matcher end() method in Java with Examples
- Matcher end(int) method in Java with Examples
- Matcher regionStart() method in Java with Examples
- Matcher start() method in Java with Examples
- Matcher hasTransparentBounds() method in Java with Examples
- Matcher start(int) method in Java with Examples
- Matcher reset() method in Java with Examples
- Matcher region(int, int) method in Java with Examples
- Matcher toString() method in Java with Examples
- Matcher requireEnd() method in Java with Examples
- Matcher hitEnd() method in Java with Examples
- Matcher hasAnchoringBounds() method in Java with Examples
- Matcher find(int) method in Java with Examples
- Matcher group(int) method in Java with Examples
- Matcher lookingAt() method in Java with Examples
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.



