package regular_expression;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Test {
public static void main(String[] args) {
Pattern pattern = Pattern.compile("^the");
Matcher matcher1 = pattern.matcher("the is");
Matcher matcher2 = pattern.matcher("there");
boolean matches1 = matcher1.matches();
boolean matches2 = matcher1.matches();
System.out.println(matches1);
System.out.println(matches2);
}
}
Pattern pattern = Pattern.compile("^the.+");
Matcher matcher1 = pattern.matcher("the is");
Matcher matcher2 = pattern.matcher("there");
boolean matches1 = matcher1.matches();
boolean matches2 = matcher1.matches();
System.out.println(matches1);
System.out.println(matches2);
.+表示the后面可以是任意值
你的正则说了一the开头,但是后面是设么规则没说,所以全部都是false,所以要修改正则为^the.*
,这样可以匹配the
,there
,等等,也就是说以the开头,后面有0位或者多位