JAVA Pattern and Matcher

JAVA Pattern and Matcher

1.Pattern
Pattern&Matcher都是用於處理正則表達式的類
Pattern通過私有構造:
Pattern p = Pattern.compile(“^d”);
pattern的匹配姿勢:
boolean b = p.matches(“inputstring”)來判斷是否整體匹配

2.Matcher
Matcher比Pattern更高級,可以用於多組查詢
通過Pattern得到。Matcher的得到方式:
Pattern p = Pattern.compile(“^d”)
Matcher m = p.matcher(“inputstring”)

matcher的匹配姿勢:
1.matches() //整體:
m.matches()
等效於
Pattern.compile(“^d”).matcher(“inputstring”).matches()
等效於
Pattern.matches(“^d”,”inputstring”);

2.find() //多組查詢
3.lookingAt() //從頭開始找

通過上面三個方法得到匹配項後
後面接上匹配項的解析方法:
1.start() //得到匹配項的起始ids
2.end() //得到匹配項的結束ids
3.group() //得到匹配項內容

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章