Matcher hitEnd() method in Java with Examples
The hitEnd() method of Matcher Class is used to check if this the matching of the pattern on this matcher has stopped or not. The matching ends when no more matched group is found in the matcher. This method returns a boolean value stating the same.
Syntax:
public boolean hitEnd()
Parameters: This method takes no parameters.
Return Value: This method returns a boolean value stating whether this matcher has finished matching or not.
Below examples illustrate the Matcher.hitEnd() 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 Geeks for For Geeks Geek" ;
Matcher matcher
= pattern.matcher(stringToBeMatched);
while (matcher.find()) {
System.out.println( "Group matched: "
+ matcher.group());
System.out.println( "Has matching hit end: "
+ matcher.hitEnd());
}
System.out.println( "Has matching hit end: "
+ matcher.hitEnd());
}
}
|
Output:
Group matched: Geeks
Has matching hit end: false
Group matched: Geeks
Has matching hit end: false
Group matched: Geeks
Has matching hit end: false
Group matched: Geeks
Has matching hit end: false
Has matching hit end: true
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 FGF GFG GFG FGF" ;
Matcher matcher
= pattern.matcher(stringToBeMatched);
while (matcher.find()) {
System.out.println( "Group matched: "
+ matcher.group());
System.out.println( "Has matching hit end: "
+ matcher.hitEnd());
}
System.out.println( "Has matching hit end: "
+ matcher.hitEnd());
}
}
|
Output:
Group matched: GFG
Has matching hit end: false
Group matched: GFG
Has matching hit end: false
Group matched: GFG
Has matching hit end: false
Group matched: GFG
Has matching hit end: false
Group matched: GFG
Has matching hit end: false
Group matched: GFG
Has matching hit end: false
Group matched: GFG
Has matching hit end: false
Has matching hit end: true
Reference: https://docs.oracle.com/javase/9/docs/api/java/util/regex/Matcher.html#hitEnd–
Last Updated :
27 Nov, 2018
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...