Java Regex裡find()方法的使用

來源:互聯網
上載者:User

標籤:

import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Test {
  public static void main (String[] args) {
    Pattern p = Pattern .compile ("\\d{3,5}");
    Matcher m = p.matcher("123-4567812-910-11");

     /*print (m.matches ());
    m.reset ();*/
    print (m.find ());//true
    print (m.group ());//123
    print (m.start ()+"-"+m.end ());//0-3:1~-
    System.out.printf("換行--------------------\n");
    print (m.find ());//true
    print (m.group ());//45678
    print (m.start ()+"-"+m.end ());//4-9:4~1
    System.out.printf("換行--------------------\n");
    print (m.find ());//true
    print (m.group ());//910
    print (m.start ()+"-"+m.end ());//12-15:9~-
    System.out.printf("換行--------------------\n");
    print (m.find ());//false
    System.out.printf("換行--------------------\n");
    System.out.println ();


    print (m.matches ());//false
    System.out.printf("換行\n");
    //m.reset ();
    print (m.find ());//true
    print (m.group ());//45678
    print (m.start ()+"-"+m.end ());//4-9
    System.out.printf("換行\n");
    print (m.find ());//true
    print (m.group ());//910
    print (m.start ()+"-"+m.end ());//12-15
    System.out.printf("換行\n");
    print (m.find ());//false
    System.out.printf("換行\n");
    print (m.find ());//false
    System.out.printf("換行\n");
    System.out.println ();

    print (m.matches ());//false
    System.out.printf("換行\n");
    m.reset ();
    print (m.find ());//true
    print (m.group ());//123
    print (m.start ()+"-"+m.end ());//0-3
    System.out.printf("換行\n");
    print (m.find ());//true
    print (m.group ());//45678
    print (m.start ()+"-"+m.end ());//4-9
    System.out.printf("換行\n");
    print (m.find ());//true
    print (m.group ());//910
    print (m.start ()+"-"+m.end ());//12-15
    System.out.printf("換行\n");
    print (m.find ());//false
    System.out.printf("換行\n");
    System.out.println ();

    print (m.lookingAt ());//true
    print (m.lookingAt ());//true
  }
  
   public static void print (Object o) {
    System.out.println (o);
  }
}
/*
*   對這個程式的解釋:這個程式最大的痛點在於find()方法的理解
*   在第一段程式中,find會一個字元一個字元的匹配,當檢測到3的時候,發現已經有3個字元,那麼這個find可以返回true了,但是沒有完,它會繼續檢測,發現3的後面是-而不是數字,第一個find的結束,所以在group這個裡輸出123,從0到3(注意下標是從0開始的)
*   第二個find從下標為3的字元開始找,也就是上次find結束的地方,那麼這個find一上來就找到-,會不會因此而返回false,答案是不會,它會繼續往後面找,看能不能在後面的字串中找到合格字串片段,很幸運,它找到了,那就是45678,因為指定的字串
*   片段的上限長度為5,所以在group輸出45678而不是4567812,那麼下標的始末為什麼是4-9而不是4-8呢?雖然字串片段的指定上限是5,但並不會因此就不繼續往後面檢查一位。
*   第三個find從下標為9的地方開始找,1,2,-,發現都不行,繼續找!9,1,0,-發現滿足條件了,返回true,同時group裡輸出910,下標又為什麼從12開始呢?因為find從下標為12處才找到滿足條件的字串片段。
*   第四個find很可憐,-,1,1都不滿足,後面又沒有字串了,所以只好返回false了、
*   為什麼加了matches和reset後輸出結果又變得不一樣了?先從matches說起,matches要求全部匹配才能返回true,但是很不幸字串的長度一看就不止5個字元,所以返回false是毫無疑問的。具體來說,matches也是一個字元一個字元檢查的,當他檢查到第一個“-”時發現
*   不是數字了,所以返回了false(但是就算全部是數字也返回false,就如上面所說的),關鍵是這個matches對後面的find有影響,因為find是從matches停止位開始檢查的,就像下一個find是從上一個find停止位開始檢查的一樣,所以輸出結果和上一段不一樣了。
*   但是加了reset後,匹配器被重設,就相當於find可以從字串的第一個字元開始檢查了。
*   lookingAt()是每一次都從頭開始檢查是不是符合要求,不管有多少個lookingAt(),而123是符合要求的,所以會輸出true
*   注意只有匹配成功,才可以通過 start、end 和 group 方法擷取更多資訊。
*/

Java Regex裡find()方法的使用

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.