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。
原文地址:https://www.cnblogs.com/liwendeboke/p/6054690.html