Matcher toString() method in Java with Examples
Last Updated :
27 Nov, 2018
The toString() method of Matcher Class is used to get the String representation of this matcher. This method is derived from the Object Class and behaves in the similar way.
Syntax:
public String toString()
Parameters: This method takes no parameters.
Return Value: This method returns a String value which is the String representation of this matcher.
Below examples illustrate the Matcher.toString() 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
= " Geeks for For Geeks Geek" ;
Matcher matcher
= pattern.matcher(stringToBeMatched);
System.out.println(matcher.toString());
}
}
|
Output:
java.util.regex.Matcher[pattern=(Geeks) region=0,25 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
= "FGF GFG GFG FGF" ;
Matcher matcher
= pattern.matcher(stringToBeMatched);
System.out.println(matcher.toString());
}
}
|
Output:
java.util.regex.Matcher[pattern=(GFG) region=0,15 lastmatch=]
Reference: https://docs.oracle.com/javase/9/docs/api/java/util/regex/Matcher.html#toString–
Share your thoughts in the comments
Please Login to comment...