1. 程式人生 > >Java正則表達式Pattern和Matcher類

Java正則表達式Pattern和Matcher類

false lines round 表達 -a 嘗試 cas target 大小

轉載自--小魚兒是壞蛋(原文鏈接)

概述

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

Pattern類
常用方法及介紹

1. Pattern complie(String regex)
由於Pattern的構造函數是私有的,不可以直接創建,所以通過靜態方法compile(String regex)方法來創建,將給定的正則表

達式編譯並賦予給Pattern類

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

例,
String regex = "\\?|\\*";
Pattern pattern = Pattern.compile(regex);
String patternStr = pattern.pattern();//返回\?\*


3. Pattern compile(String regex, int flags) 方法功能和compile(String regex)相同,不過增加了flag參數

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

Pattern.CANON_EQ 當且僅當兩個字符的”正規分解(canonical decomposition)”都完全相同的情況下,才認定匹配.比如


用了這個標誌之後,表達式”a\u030A”會匹配”?”.默認情況下,不考慮”規範相等性(canonical equivalence)”.

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

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


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

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

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


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


4. Pattern.matcher(CharSequence input) 對指定輸入的字符串創建一個Matcher對象

例,
Pattern pattern = Pattern.compile("\\?{2}");
Matcher matcher = pattern.matcher("??");
boolean matches = matcher.matches();// true


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

例,
String regex = ",";
Pattern pattern = Pattern.compile(regex);
//這裏必須用數組形式,因為split分割後默認保存為數組
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



6. 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


7. matches()方法編譯給定的正則表達式並且對輸入的字串以該正則表達式為模開展匹配,該方法適合於該正則表達式只會使用一次的情況,也就是只進行一次匹配工作,因為這種情況下並不需要生成一個Matcher實例.

例,
String regex = "\\?|\\*";
Pattern pattern = Pattern.compile(regex);
boolean matches = pattern.matches(regex, "?");//返回true


*******************************************
Matcher類
常用方法及介紹

1. 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);



2. 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



3. 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


4. int start() 返回當前匹配到的字符串在原目標字符串中的位置
int end() 返回當前匹配的字符串的最後一個字符在原目標字符串中的索引位置.
String group() 返回匹配到的子字符串
Pattern.start(),Pattern.end(),Pattern.group()代碼示例
註意:start(),end()和group()方法必須放在find()方法後面!!!

例,
Pattern p=Pattern.compile("wo");
Matcher m=p.matcher("hello,world");
m.find();//這裏必須有,不然後面的start()和group()方法會報錯
System.out.println(m.start());//6
System.out.println(m.group());// wo

Java正則表達式Pattern和Matcher類