String regex1 = "^dog$";
String regex2 = "dog";
Pattern pattern1 = Pattern.compile(regex1);
Pattern pattern2 = Pattern.compile(regex2);
Matcher matcher1 = pattern1.matcher("dog");
Matcher matcher2 = pattern1.matcher("doggie");
Matcher matcher3 = pattern2.matcher("dog");
Matcher matcher4 = pattern2.matcher("doggie");
System.out.println(matcher1.matches());
System.out.println(matcher2.matches());
System.out.println(matcher3.matches());
System.out.println(matcher4.matches());
输出:
true
false
true
false
答案 0 :(得分:2)
答案 1 :(得分:0)
第一个和第二个正则表达式应完全相同:
String regex1 = "^dog$";
表示start(^)和end($)
之间的字符串String regex2 = "dog";
意味着同样的事情。
如果您想匹配doggie
,请使用一些通配符,例如?
或*
或g+
等...