Matcher matches() method in Java with Examples
Last Updated :
26 Nov, 2018
The matches() method of Matcher Class is used to get the result whether this pattern matches with this matcher or not. It returns a boolean value showing the same.
Syntax:
public boolean matches()
Parameters: This method do not takes any parameter.
Return Value: This method returns a boolean value showing whether this pattern matches with this matcher or not.
Below examples illustrate the Matcher.matches() 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.matches());
}
}
|
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.matches());
}
}
|
Reference: Oracle Doc
Share your thoughts in the comments
Please Login to comment...