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 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
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
String regex = "\\?|\\*"; Pattern pattern = Pattern.compile(regex); boolean matches = pattern.matches(regex, "?");//返回true
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);
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
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
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自行學習或者參考其餘博客.