The requireEnd() method of Matcher Class is used to check if any combination of anchors has caused the match to be bounded at the end. These anchors can be any anchor like a word anchor, for instance, or a lookahead. This method returns a boolean value stating the same.
Syntax:
public boolean requireEnd()
Parameters: This method takes no parameters.
Return Value: This method returns a boolean value stating whether if any combination of anchors has caused the match to be bounded at the end.
Below examples illustrate the Matcher.requireEnd() method:
Example 1:
// Java code to illustrate requireEnd() method import java.util.regex.*; public class GFG { public static void main(String[] args) { // Get the regex to be checked // with an anchor String regex = "Geeks$"; // Create a pattern from regex Pattern pattern = Pattern.compile(regex); // Get the String to be matched String stringToBeMatched = "GFG GFG GEEKS Geeks"; // Create a matcher for the input String Matcher matcher = pattern.matcher(stringToBeMatched); matcher.find(); // Check if a match has been found // using requireEnd() method System.out.println("Has any anchor " + "bounded the search: " + matcher.requireEnd()); } } |
Has any anchor bounded the search: true
Example 2:
// Java code to illustrate requireEnd() method import java.util.regex.*; public class GFG { public static void main(String[] args) { // Get the regex to be checked // without any anchor String regex = "Geeks"; // Create a pattern from regex Pattern pattern = Pattern.compile(regex); // Get the String to be matched String stringToBeMatched = "GFG GFG GEEKS Geeks"; // Create a matcher for the input String Matcher matcher = pattern.matcher(stringToBeMatched); matcher.find(); // Check if a match has been found // using requireEnd() method System.out.println("Has any anchor " + "bounded the search: " + matcher.requireEnd()); } } |
Has any anchor bounded the search: false
Reference: https://docs.oracle.com/javase/9/docs/api/java/util/regex/Matcher.html#requireEnd–
Attention reader! Don’t stop learning now. Get hold of all the important Java and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready.
Recommended Posts:
- Matcher quoteReplacement(String) method in Java with Examples
- Matcher pattern() method in Java with Examples
- Matcher reset() method in Java with Examples
- Matcher reset(CharSequence) method in Java with Examples
- Matcher start() method in Java with Examples
- Matcher start(int) method in Java with Examples
- Matcher start(String) method in Java with Examples
- Matcher end(int) method in Java with Examples
- Matcher end() method in Java with Examples
- Matcher group() method in Java with Examples
- Matcher group(String) method in Java with Examples
- Matcher groupCount() method in Java with Examples
- Matcher toMatchResult() method in Java with Examples
- Matcher matches() method in Java with Examples
- Matcher find() method in Java with Examples
- Matcher find(int) method in Java with Examples
- Matcher usePattern(Pattern) method in Java with Examples
- Matcher group(int) method in Java with Examples
- Matcher lookingAt() method in Java with Examples
- Matcher replaceAll(String) method in Java with Examples
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

