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 新老师祝福语 简短独特
14 中秋祝福语给父亲简短
15 红包祝福语简短网名
16 升职祝福语朋友简短精辟
17 给男生的简短祝福语
18 收到公公生日祝福语简短