Java中具有示例的Matcher hitEnd()方法
该java.util.regex.Matcher中的类代表一个引擎,进行各种匹配操作。该类没有构造函数,可以使用matches()类java.util.regex.Pattern的方法创建/获取该类的对象。
该hitEnd()方法,检验是否如果是这样,则返回true,否则返回false以前的比赛中达到输入数据的末尾。如果此方法返回true,则表明更多输入数据可能会更改匹配结果。
例如,如果您尝试使用正则表达式“you$”将输入字符串的最后一个单词与您匹配,并且如果您的第一个输入行是“你好,你好吗”,则可能有匹配项,但是,如果您接受更多句子新行的最后一个单词可能不是必需的单词(即“you”),从而使匹配结果为假。在这种情况下,该hitEnd()方法返回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$";
//从用户读取输入
Scanner sc = new Scanner(System.in);
System.out.println("Enter input text: ");
String input = sc.nextLine();
//实例化Pattern类
Pattern pattern = Pattern.compile(regex);
//实例化Matcher类
Matcher matcher = pattern.matcher(input);
//验证是否发生匹配
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");
} else {
System.out.println("The result of the match will be true, inspite of more data");
}
}
}输出结果
Enter input text: Hello how are you Match found More input may turn the result of the match false
热门推荐
10 同事换单位简短祝福语
11 谢谢朋友的祝福语简短
12 立秋节气简短祝福语
13 新房祝福语简短10字
14 长辈新年祝福语大全简短
15 新年给大家简短祝福语
16 生日祝福语简短独特姑姑
17 芭比的婚礼祝福语简短
18 邀请新人祝福语简短英文