java Pattern和Matcher完全解析


基本使用:

本文不講解正則表達式,需要請看API

 

Scanner中的使用正則表達式

  //Scanner 支持的分組
        Scanner  cin=new Scanner("red a bbc").useDelimiter("\\s*a\\s*");
        System.out.println(cin.next());
        System.out.println(cin.next());
out:

red
bbc

等同於下面代碼

//等於 正則
Scanner  cin2=new Scanner("red a bbc");
cin2.findInLine("\\s*"); // findLine 允許存在多個,match()為最終需要匹配的字符串
MatchResult result = cin2.match();
for (int i = 0; i < result.groupCount(); i++) {
    System.out.println(result.group(i));
}

 

Pattern:

 //基本匹配
        boolean b = Pattern.matches("a*b", "aaaab");
        System.out.println(b);

 

String的aplit的實現

//按照數字分割
        Pattern p=Pattern.compile("\\d+");
        String[] str=p.split("好456456像:0532214是");
        for (int i = 0; i < str.length; i++) {
            System.out.println(str[i]);

        }

一般使用Pattern.matches(String regex,CharSequence input)是一個靜態方法,用於快速匹配字符串,該方法適合用於只匹配一次,且匹配全部字符串. 

Java代碼示例: 
Pattern.matches("\\d+","2223");//返回true 
Pattern.matches("\\d+","2223aa");//返回false,需要匹配到所有字符串才能返回true,這里aa不能匹配到 
Pattern.matches("\\d+","22bb23");//返回false,需要匹配到所有字符串才能返回true,這里bb不能匹配到 

Pattern p=Pattern.compile("\\d+"); 
Matcher m=p.matcher("22bb23"); 

m.pattern();//返回p 也就是返回該Matcher對象是由哪個Pattern對象的創建的 
 

 

重點:

  • matches 方法嘗試將整個輸入序列與該模式匹配。

  • lookingAt 嘗試將輸入序列從頭開始與該模式匹配。

  • find 方法掃描輸入序列以查找與該模式匹配的下一個子序列。  

 

  // matches()對整個字符串進行匹配,只有整個字符串都匹配了才返回true
        Pattern p=Pattern.compile("\\d+");
        Matcher m=p.matcher("22bb23");
        m.matches();//返回false,因為bb不能被\d+匹配,導致整個字符串匹配未成功.
        Matcher m2=p.matcher("2223");
        m2.matches();//返回true,因為\d+匹配到了整個字符串

       // lookingAt()對前面的字符串進行匹配,只有匹配到的字符串在最前面才返回true
        Pattern p1=Pattern.compile("\\d+");
        Matcher m3=p1.matcher("22bb23");
        m.lookingAt();//返回true,因為\d+匹配到了前面的22
        Matcher m4=p1.matcher("aa2223");
        m2.lookingAt();//返回false,因為\d+不能匹配前面的aa

       // find()對字符串進行匹配,匹配到的字符串可以在任何位置.
        Pattern p2=Pattern.compile("\\d+");
        Matcher m5=p2.matcher("22bb23");
        m.find();//返回true
        Matcher m6=p2.matcher("aa2223");
        m2.find();//返回true
        Matcher m7=p2.matcher("aa2223bb");
        m3.find();//返回true
        Matcher m8=p2.matcher("aabb");
        m4.find();//返回false

Mathcer.start()/ Matcher.end()/ Matcher.group() 
當使用matches(),lookingAt(),find()執行匹配操作后,就可以利用以上三個方法得到更詳細的信息. 

  1. start()返回匹配到的子字符串在字符串中的索引位置.  
  2. end()返回匹配到的子字符串的最后一個字符在字符串中的索引位置.  即為最后位置加一
  3. group()返回匹配到的子字符串 

 

Java代碼示例: 
Pattern p=Pattern.compile("\\d+"); 
Matcher m=p.matcher("aaa2223bb"); 
m.find();//匹配2223 
m.start();//返回3 
m.end();//返回7,返回的是2223后的索引號 
m.group();//返回2223 

Mathcer m2=p.matcher("2223bb"); 
m2.lookingAt();   //匹配2223 
m2.start();   //返回0,由於lookingAt()只能匹配前面的字符串,所以當使用lookingAt()匹配時,start()方法總是返回0 
m2.end();   //返回4 
m2.group();   //返回2223 

Matcher m3=p.matcher("2223"); //如果Matcher m3=p.matcher("2223bb"); 那么下面的方法出錯,因為不匹配返回false
m3.matches();   //匹配整個字符串 
m3.start();   //返回0
m3.end();   //返回3,原因相信大家也清楚了,因為matches()需要匹配所有字符串 
m3.group();   //返回2223
說了這么多,相信大家都明白了以上幾個方法的使用,該說說正則表達式的分組在java中是怎么使用的. 
start(),end(),group()均有一個重載方法它們是start(int i),end(int i),group(int i)專用於分組操作,Mathcer類還有一個groupCount()用於返回有多少組. 
Java代碼示例: 
Pattern p=Pattern.compile("([a-z]+)(\\d+)"); 
Matcher m=p.matcher("aaa2223bb"); 
m.find();   //匹配aaa2223 
m.groupCount();   //返回2,因為有2組 
m.start(1);   //返回0 返回第一組匹配到的子字符串在字符串中的索引號 
m.start(2);   //返回3 
m.end(1);   //返回3 返回第一組匹配到的子字符串的最后一個字符在字符串中的索引位置. 
m.end(2);   //返回7 
m.group(1);   //返回aaa,返回第一組匹配到的子字符串 
m.group(2);   //返回2223,返回第二組匹配到的子字符串 

驗證手機號

// 驗證手機號
        Pattern compile = Pattern.compile("^[1][3,4,5,7,8][0-9]{9}$");
        Matcher matcher1 = compile.matcher("15071089603");
        while(matcher1.find()){
            System.out.println(matcher1.group());
        }

/**
      * 驗證手機號碼
      *
      * 移動號碼段:139、138、137、136、135、134、150、151、152、157、158、159、182、183、187、188、147、182
      * 聯通號碼段:130、131、132、136、185、186、145
      * 電信號碼段:133、153、180、189、177
      *
      */
 String regex = "^((13[0-9])|(14[5|7])|(15([0-3]|[5-9]))|(18[0,1,2,5-9])|(177))\\d{8}$";    

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM