java 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。
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章