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
热门推荐
5 短祝福语简短暖心
10 结婚祝福语粤语大全简短
11 晚上祝福语女生文案简短
12 法语妈妈生日祝福语简短
13 药厂开工祝福语大全简短
14 蛋糕节日祝福语简短英文
15 跨年的生日祝福语简短
16 文案祝福语英文短句简短
17 在家聚餐婚礼祝福语简短
18 学生节祝福语大全简短