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。
posted @ 2016-11-11 16:21  旧爱久居于心  阅读(2104)  评论(0编辑  收藏  举报