Matcher toMatchResult() method in Java with Examples
Last Updated :
26 Nov, 2018
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:
import java.util.regex.*;
public class GFG {
public static void main(String[] args)
{
String regex = "Geeks" ;
Pattern pattern
= Pattern.compile(regex);
String stringToBeMatched
= "GeeksForGeeks" ;
Matcher matcher
= pattern
.matcher(stringToBeMatched);
System.out.println( "Result: "
+ matcher.toMatchResult());
}
}
|
Output:
Result: java.util.regex.Matcher[pattern=Geeks region=0, 13 lastmatch=]
Example 2:
import java.util.regex.*;
public class GFG {
public static void main(String[] args)
{
String regex = "GFG" ;
Pattern pattern
= Pattern.compile(regex);
String stringToBeMatched
= "GFGFGFGFGFGFGFGFGFG" ;
Matcher matcher
= pattern
.matcher(stringToBeMatched);
System.out.println( "Result: "
+ matcher.toMatchResult());
}
}
|
Output:
Result: java.util.regex.Matcher[pattern=GFG region=0, 19 lastmatch=]
Reference: Oracle Doc
Share your thoughts in the comments
Please Login to comment...