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 祝福语驱散霉运的话简短