matches()方法

时间:2023-03-09 23:28:43
matches()方法
java.lang包中的String类和java.util.regex包中的Pattern,Matcher类中都有matches()方法,都与正则表达式有关。
下面我分别举例:(字符串:"abc",正则表达式: "[a-z]{3}"

String类的方法:
boolean b = "abc".matches("[a-z]{3}"
System.out.println(b); Pattern类中的方法:
boolean b = Pattern.matches("[a-z]{3}","abc");
System.out.println(b); Matcher类中的方法:
Pattern p = Pattern.compile("[a-z]{3}");
Matcher m = p.matcher("acc");
boolean b =m.matches()
System.out.println(b); 得到的结果都为true。