Java正則表達式Pattern和Matcher的通常用法

一.方法說明:html

find()方法是部分匹配,在部分匹配時和徹底匹配時返回true,匹配不上返回false。若是該匹配的串有組還可使用group()函數,來獲取串中的各個groupjava

matches()是所有匹配,它只有在徹底匹配時返回true,匹配不上和部分匹配都返回false。e。若是要驗證一個輸入的數據是否爲所有爲數字類型或其餘類型,通常要用matches()。正則表達式

 

二.通常寫法express

Pattern pattern= Pattern.compile(".*?,(.*)");
Matcher matcher = pattern.matcher(result);
if (matcher.find()) {
   return matcher.group(1);
}

 

三.詳解:數組

matches
public static boolean matches(String regex,  CharSequence input)app

編譯給定正則表達式並嘗試將給定輸入與其匹配。  
調用此便捷方法的形式  
Pattern.matches(regex, input);
Pattern.compile(regex).matcher(input).matches() ; 
若是要屢次使用一種模式,編譯一次後重用此模式比每次都調用此方法效率更高。
參數:
regex - 要編譯的表達式
input - 要匹配的字符序列  
拋出:  
PatternSyntaxException - 若是表達式的語法無效

find
public boolean find()嘗試查找與該模式匹配的輸入序列的下一個子序列。  
此方法從匹配器區域的開頭開始,每次調用find(),都會從上一次find()成功匹配的位置繼續向後查找,直到到達字符串末尾。第一次執行find()將從字符串開始位置查找。若是第一次調用find()返回true而第二次調用返回false,那說明字符串中只有一處與所給的正則表達式匹配。  
若是匹配成功,則能夠經過 start、end 和 group 方法獲取更多信息。  函數

matcher.start() 返回匹配到的子字符串在字符串中的索引位置. 
matcher.end()返回匹配到的子字符串的最後一個字符在字符串中的索引位置. 
matcher.group()返回匹配到的子字符串 
返回:
當且僅當輸入序列的子序列匹配此匹配器的模式時才返回 true。spa

 

四.部分JAVA正則表達式實例

   ①字符匹配 3d

Pattern p = Pattern.compile(expression); // 正則表達式 
Matcher m = p.matcher(str); // 操做的字符串 
boolean b = m.matches(); //返回是否匹配的結果 
System.out.println(b); 

Pattern p = Pattern.compile(expression); // 正則表達式 
Matcher m = p.matcher(str); // 操做的字符串 
boolean b = m. lookingAt (); //返回是否匹配的結果 
System.out.println(b); 

Pattern p = Pattern.compile(expression); // 正則表達式 
Matcher m = p.matcher(str); // 操做的字符串 
boolean b = m..find (); //返回是否匹配的結果 
System.out.println(b); 


②分割字符串 code

Pattern pattern = Pattern.compile(expression); //正則表達式 
String[] strs = pattern.split(str); //操做字符串 獲得返回的字符串數組 


③替換字符串 

Pattern p = Pattern.compile(expression); // 正則表達式 
Matcher m = p.matcher(text); // 操做的字符串 
String s = m.replaceAll(str); //替換後的字符串 



④查找替換指定字符串 

Pattern p = Pattern.compile(expression); // 正則表達式 
Matcher m = p.matcher(text); // 操做的字符串 
StringBuffer sb = new StringBuffer(); 
int i = 0; 
while (m.find()) { 
    m.appendReplacement(sb, str); 
    i++;    //字符串出現次數 
} 
m.appendTail(sb);//從截取點將後面的字符串接上 
String s = sb.toString(); 


⑤查找輸出字符串 

Pattern p = Pattern.compile(expression); // 正則表達式 
Matcher m = p.matcher(text); // 操做的字符串 
while (m.find()) { 
   matcher.start() ;
   matcher.end();
   matcher.group(1);
}

 

五.group的說明

group(),group(int i),groupcount() 這三個方法的區別,須要理解捕獲組的意思。捕獲組也就是Pattern中用()分割的子模式

以示例說明:

Pattern p = Pattern.compile("(ca)(t)");
Matcher m = p.matcher("one cat,two cats in the yard");
System.out.println("該次查找得到匹配組的數量爲:" + m.groupCount()); // 2
for (int i = 0; i <= m.groupCount(); i++) {
	System.out.println("第" + i + "組的子串內容爲:" + m.group(i));
}

輸出:

該次查找得到匹配組的數量爲:2
第0組的子串內容爲:cat
第1組的子串內容爲:ca
第2組的子串內容爲:t

能夠這樣理解:

m.groupCount()表示Pattern.compile()中的正則表達式中的()的個數。

m.group(0),等效於 m.group()  。是指輸入序列str匹配整個模式,所以爲cat

m.group(1)表示匹配正則表達式中的第一個括號裏的內容便可,所以爲ca,注意,也是第一次的值

m.group(2)表示匹配正則表達式中的第二個括號裏的內容便可,所以爲t,注意,也是第一次的值

groupcount() 返回的是正則表達式的捕獲分組的數量(捕獲分組和非捕獲分組是另外的知識點),groupcount() 的結果並不能說明匹配的結果。

group是針對正則表達式中裏面的()來講的,group(0)就是指的知足正則表達式的整個串,group(1) 指的是指正則表達式中第一個括號裏的東西,group(2)指的正則表達式中第二個括號裏的東西

示例代碼:

public class  MailTest{
    public static void main(String[] args) throws Exception{
        
        String regEx = "count(\\d+)(df)(df)*";  
        String s = "count000dfsdffcount123dfdfdfdfaaaa1";  //有兩個匹配
        Pattern pat = Pattern.compile(regEx);  
        Matcher mat = pat.matcher(s); 
        if(mat.find()){
           int groupCount = mat.groupCount();
           System.out.println(groupCount);
           for(int i = 0;i <= groupCount;i++) {
        	   System.out.println(mat.group(i));
           }
        }
        System.out.println();
        if(mat.find()){
            int groupCount = mat.groupCount();
            System.out.println(groupCount);
            for(int i = 0;i <= groupCount;i++) {
         	   System.out.println(mat.group(i));
            }
            //count(\\d+)(df)(df)*-->count(\\d+)(df)(df)(df)(df)
            //groupCount()僅是從正則表達式裏面捕獲分組的數量(實際可見的"()"的個數),因此下面group(4)和5報錯
            //System.out.println(mat.group(4));//好像看起來第二個匹配串中有4
            //System.out.println(mat.group(5));//好像看起來第二個匹配串中有5
         }
    }
}

輸出結果:

3 //正則表達式裏面有三個括號count(\\d+)(df)(df)*,因此groupCount始終爲3,groupCount僅僅是正則表達式裏面的()
count000df //匹配正則表達式的整個竄
000 //正則表達式裏面第一個括號(\\d+)
df  //正則表達式裏面第二個括號(df)
null

3
count123dfdfdfdf
123
df
df

若是正則表達式裏面沒有來分組的"()"號,那麼groupCount就爲0,例如:

public class  MailTest{
    public static void main(String[] args) throws Exception{
        
        String regEx = "count\\d+";  
        String s = "count000dfsdffcount123dfdfdfdfaaaa1";  //有兩個匹配
        Pattern pat = Pattern.compile(regEx);  
        Matcher mat = pat.matcher(s); 
        if(mat.find()){
           int groupCount = mat.groupCount();
           System.out.println(groupCount);
           for(int i = 0;i <= groupCount;i++) {
        	   System.out.println(mat.group(i));
           }
        }
        System.out.println();
        if(mat.find()){
            int groupCount = mat.groupCount();
            System.out.println(groupCount);
            for(int i = 0;i <= groupCount;i++) {
         	   System.out.println(mat.group(i));
            }
         }
    }
}

輸出結果:

0
count000

0
count123
相關文章
相關標籤/搜索