English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
Thisfrom java.util.regex.Matcher'sThis class represents an engine for performing various matching operations. The class has no constructor and can be usedmatches()
method of the class java.util.regex.Pattern creates/gets an object of this class.
ThishitEnd()method checks whether it is the end of the input data, and returns true if so, otherwise returns false. If this method returns true, it indicates that more input data may change the match result.
For example, if you try to use the regular expression "you $" to match the last word of the input string with what you are matching, and if your first input line is "Hello, how are you?", there may be a match, but if you accept more sentences with new lines, the last word may not be the necessary word (i.e., "you"), making the match result false. In this case, thehitEnd()
The method returns true.
import java.util.Scanner; import java.util.regex.Matcher; import java.util.regex.Pattern; public class HitEndExample { public static void main(String args[]) { String regex = "you$"; //Read input from the user Scanner sc = new Scanner(System.in); System.out.println("Enter input text: "); String input = sc.nextLine(); //Instantiate Pattern class Pattern pattern = Pattern.compile(regex); //Instantiate Matcher class Matcher matcher = pattern.matcher(input); //Verify whether a match has occurred if(matcher.find()) { System.out.println("Match found"); } boolean result = matcher.hitEnd(); if(result) { System.out.println("More input may turn the result of the match false"); } System.out.println("The result of the match will be true, despite more data"); } } }
Output result
Enter input text: Hello, how are you Match found More input may turn the result of the match false