The toMatchResult() method of Matcher Class is used to get the current result state of this Matcher.
Syntax:
public MatchResult toMatchResult()
Parameters: This method do not takes any parameter.
Return Value: This method returns a MatchResult with the current match result state of this Matcher.
Below examples illustrate the Matcher.toMatchResult() method:
Example 1:
// Java code to illustrate toMatchResult() 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 result state // using toMatchResult() method System.out.println("Result: " + matcher.toMatchResult()); } } |
Result: java.util.regex.Matcher[pattern=Geeks region=0, 13 lastmatch=]
Example 2:
// Java code to illustrate toMatchResult() 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 .matcher(stringToBeMatched); // Get the result state // using toMatchResult() method System.out.println("Result: " + matcher.toMatchResult()); } } |
Result: java.util.regex.Matcher[pattern=GFG region=0, 19 lastmatch=]
Reference: Oracle Doc
Attention reader! Don’t stop learning now. Get hold of all the important Java and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready.
Recommended Posts:
- Matcher quoteReplacement(String) method in Java with Examples
- Matcher pattern() method in Java with Examples
- Matcher reset() method in Java with Examples
- Matcher reset(CharSequence) method in Java with Examples
- Matcher start() method in Java with Examples
- Matcher start(int) method in Java with Examples
- Matcher start(String) method in Java with Examples
- Matcher end(int) method in Java with Examples
- Matcher end() method in Java with Examples
- Matcher group() method in Java with Examples
- Matcher group(String) method in Java with Examples
- Matcher groupCount() method in Java with Examples
- Matcher matches() method in Java with Examples
- Matcher find() method in Java with Examples
- Matcher find(int) method in Java with Examples
- Matcher usePattern(Pattern) method in Java with Examples
- Matcher group(int) method in Java with Examples
- Matcher lookingAt() method in Java with Examples
- Matcher replaceAll(String) method in Java with Examples
- Matcher replaceAll(Function) 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.

