正則表達式處理:Pattern和Matcher類的使用

模式對象和匹配器對象的基本使用

把規則轉換爲模式對象

Pattern p = Pattern.compile("a*b");

 通過模式對象得到匹配器對象

Matcher m = p.matcher("aaaaab");
通過匹配器對象調用功能
boolean b = m.matches();
System.out.println(b);

作爲判斷來說
String s = "aaaaab";
String regex = "a*b";
boolean flag = s.matches(regex);

System.out.println(flag);

使用例子:

Pattern p = Pattern.compile(regex);
Matcher m = p.matcher(s);

具體可以參考如下博客,講的很詳細:

http://blog.csdn.net/yin380697242/article/details/52049999

http://blog.csdn.net/woniu317/article/details/52186694


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