
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Matcher requireEnd Method in Java with Examples
The java.util.regex.Matcher class represents an engine that performs various match operations. There is no constructor for this class, you can create/obtain an object of this class using the matches() method of the class java.util.regex.Pattern.
In case of a match, the requireEnd() method of this (Matcher) class verifies whether there is a chance for the match result to be false, in case of more input, if so, this method returns true else it returns false.
For example, If you are trying to match the last word of an input string to you using the regex “you$” and if your 1st input line is “hello how are you” you might have a match but, if you accept more sentences the last word of the new line(s) may not be the required word (which is “you”), making your match result false. In such cases, the requiredEnd() method returns true.
Similarly, if you are trying to match a particular character in the input say # and if your 1st input line is “Hello # how are you” you will have a match and, more input data may change the contents of the matcher but it doesn't change the result which is true. In such a scenario the requiredEnd() method returns false.
Example 1
import java.util.Scanner; import java.util.regex.Matcher; import java.util.regex.Pattern; public class RequiredEndExample { public static void main( String args[] ) { String regex = "you$"; //Reading input from user Scanner sc = new Scanner(System.in); System.out.println("Enter input text: "); String input = sc.nextLine(); //Instantiating the Pattern class Pattern pattern = Pattern.compile(regex); //Instantiating the Matcher class Matcher matcher = pattern.matcher(input); //verifying whether a match occurred if(matcher.find()) { System.out.println("Match found"); } boolean result = matcher.requireEnd(); if(result) { System.out.println("More input may turn the result of the match false"); } else{ System.out.println("The result of the match will be true, inspite of more data"); } } }
Output
Enter input text: Hello how are you Match found More input may turn the result of the match false
Example 2
import java.util.Scanner; import java.util.regex.Matcher; import java.util.regex.Pattern; public class RequiredEndExample { public static void main( String args[] ) { String regex = "[#]"; //Reading input from user Scanner sc = new Scanner(System.in); System.out.println("Enter input text: "); String input = sc.nextLine(); //Instantiating the Pattern class Pattern pattern = Pattern.compile(regex); //Instantiating the Matcher class Matcher matcher = pattern.matcher(input); //verifying whether a match occurred if(matcher.find()) { System.out.println("Match found"); } boolean result = matcher.requireEnd(); if(result) { System.out.println("More input may turn the result of the match false"); } else{ System.out.println("The result of the match will be true, inspite of more data"); } } }
Output
Enter input text: Hello# how# are you Match found The result of the match will be true, in spite of more data