Java Pattern And Matcher

概述

  • Pattern類的做用在於編譯正則表達式後建立一個匹配模式.
  • Matcher類使用Pattern實例提供的模式信息對正則表達式進行匹配

Pattern類

經常使用方法及介紹

  • Pattern complie(String regex) 
    因爲Pattern的構造函數是私有的,不能夠直接建立,因此經過靜態方法compile(String regex)方法來建立,將給定的正則表達式編譯並賦予給Pattern類正則表達式

  • String pattern() 返回正則表達式的字符串形式,其實就是返回Pattern.complile(String regex)的regex參數函數

String regex = "\\?|\\*";
Pattern pattern = Pattern.compile(regex);
String patternStr = pattern.pattern();//返回\?\*
  • Pattern compile(String regex, int flags) 方法功能和compile(String regex)相同,不過增長了flag參數學習

  • int flags() 返回當前Pattern的匹配flag參數. 
    flag參數用來控制正則表達式的匹配行爲,可取值範圍以下:ui

Pattern.CANON_EQ 當且僅當兩個字符的」正規分解(canonical decomposition)」都徹底相同的狀況下,才認定匹配.好比用了這個標誌以後,表達式」a\u030A」會匹配」?」.默認狀況下,不考慮」規範相等性(canonical equivalence)」.spa

Pattern.CASE_INSENSITIVE(?i) 默認狀況下,大小寫不明感的匹配只適用於US-ASCII字符集.這個標誌能讓表達式忽略大小寫進行匹配.要想對Unicode字符進行大小不明感的匹 配,只要將UNICODE_CASE與這個標誌合起來就好了..net

Pattern.COMMENTS(?x) 在這種模式下,匹配時會忽略(正則表達式裏的)空格字符(譯者注:不是指表達式裏的」\s」,而是指表達式裏的空格,tab,回車之類).註釋從#開始,一直到這行結束.能夠經過嵌入式的標誌來啓用Unix行模式.code

Pattern.DOTALL(?s)在這種模式下,表達式’.’能夠匹配任意字符,包括表示一行的結束符。默認狀況下,表達式’.’不匹配行的結束符.對象

Pattern.MULTILINE(?m)在這種模式下,’\^’和’$’分別匹配一行的開始和結束.此外,’^’仍然匹配字符串的開始,’$’也匹配字符串的結束.默認狀況下,這兩個表達式僅僅匹配字符串的開始和結束.blog

Pattern.UNICODE_CASE(?u) 在這個模式下,若是你還啓用了CASE_INSENSITIVE標誌,那麼它會對Unicode字符進行大小寫不明感的匹配.默認狀況下,大小寫不敏感的匹配只適用於US-ASCII字符集.索引

Pattern.UNIX_LINES(?d) 在這個模式下,只有’\n’才被認做一行的停止,而且與’.’,’^’,以及’$’進行匹配.

  • Pattern.matcher(CharSequence input) 對指定輸入的字符串建立一個Matcher對象
Pattern pattern = Pattern.compile("\\?{2}");
Matcher matcher = pattern.matcher("??");
boolean matches = matcher.matches();// true
  • String[] split(CharSequence input) 
    String[] split(CharSequence input, int limit)

  • String[] split(CharSequence input, int limit) 
    功能和String[] split(CharSequence input)相同,增長參數limit目的在於要指定分割的段數

String regex = "\\?|\\*";
Pattern pattern = Pattern.compile(regex);
String[] splitStrs = pattern.split("123?123*456*456");//123 123 456 456
String[] splitStrs2 = pattern.split("123?123*456*456", 2);// 123 123*456*456
  • Pattern.quote(String s) 返回給定的字符串的字面量,關於方法的具體信息請參考123
String pattern = Pattern.quote("1252343% 8 567 hdfg gf^$545");
System.out.println("Pattern is : "+pattern);

輸出信息:

Pattern is : \Q1252343% 8 567 hdfg gf^$545\E

  • matches()方法編譯給定的正則表達式而且對輸入的字串以該正則表達式爲模開展匹配,該方法適合於該正則表達式只會使用一次的狀況,也就是隻進行一次匹配工做,由於這種狀況下並不須要生成一個Matcher實例.
String regex = "\\?|\\*";
Pattern pattern = Pattern.compile(regex);
boolean matches = pattern.matches(regex, "?");//返回true

Matcher類

經常使用方法及介紹

  • boolean matches() 最經常使用方法:嘗試對整個目標字符展開匹配檢測,也就是隻有整個目標字符串徹底匹配時才返回真值.
Pattern pattern = Pattern.compile("\\?{2}");
Matcher matcher = pattern.matcher("??");
boolean matches = matcher.matches();//true
System.out.println(matches);
matcher=pattern.matcher("?");
matches = matcher.matches();//false
System.out.println(matches);
  • boolean lookingAt() 對前面的字符串進行匹配,只有匹配到的字符串在最前面纔會返回true
Pattern p = Pattern.compile("\\d+");
Matcher m = p.matcher("22bb23");
boolean match = m.lookingAt();//true
System.out.println(match);
m = p.matcher("bb2233");
match= m.lookingAt();
System.out.println(match);//false
  • boolean find() 對字符串進行匹配,匹配到的字符串能夠在任何位置
Pattern p = Pattern.compile("\\d+");
Matcher m = p.matcher("22bb23");
m.find();// 返回true
Matcher m2 = p.matcher("aa2223");
m2.find();// 返回true
Matcher m3 = p.matcher("aa2223bb");
m3.find();// 返回true
Matcher m4 = p.matcher("aabb");
m4.find();// 返回false
  • int start() 返回當前匹配到的字符串在原目標字符串中的位置
  • int end() 返回當前匹配的字符串的最後一個字符在原目標字符串中的索引位置.
  • String group() 返回匹配到的子字符串 
    Pattern.start(),Pattern.end(),Pattern.group()代碼示例
Pattern p = Pattern.compile("\\d+");
Matcher m = p.matcher("aa22bb23");
m.find();
int start = m.start();//2
String group = m.group();//22
int end = m.end();//4
System.out.println(start);
System.out.println(group);
System.out.println(end);

還有一些其餘經常使用的方法,請參考API自行學習或者參考其餘博客.

相關文章
相關標籤/搜索