The find() method of Matcher Class attempts to find the next subsequence of the input sequence that find the pattern. It returns a boolean value showing the same.
Syntax:
public boolean find()
Parameters: This method do not takes any parameter.
Return Value: This method returns a boolean value showing whether a subsequence of the input sequence find this matcher’s pattern
Below examples illustrate the Matcher.find() 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(matcher.find());
}
}
|
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(matcher.find());
}
}
|
Reference: Oracle Doc