正則表達式的貪婪匹配和非貪婪匹配

本文轉自 http://blog.csdn.net/hellobobantang/article/details/7048240
               http://itlab.idcquan.com/Java/base/773980.html


By default, pattern matching is greedy, which means that the matcher returns the longest match possible. For example, applying the patternA.*c toAbcAbcAmatchesAbcAbc rather than the shorterAbc. To do nongreedy matching, a question mark must be added to the quantifier. For example, the patternA.*?c will find the shortest match possible.
// Greedy quantifiers
 String match = find("A.*c","AbcAbc"); // AbcAbc
// Nongreedy quantifiers
 match = find("A.*?c","AbcAbc"); // Abc

關於?= 

http://topic.csdn.net/u/20100902/16/9a8f2249-86f2-4f80-b5f5-dbf8769ba674.html


貪婪量詞:

先看整個字符串是不是一個匹配。如果沒有發現匹配,它去掉最後字符串中的最後一個字符,並再次嘗試。如果還是沒有發現匹配,那麼    再次去掉最後一個字符串,這個過程會一直重複直到發現一個匹配或者字符串不剩任何字符。簡單量詞都是貪婪量詞。

    惰性量詞:

先看字符串中的第一個字母是不是一個匹配,如果單獨着一個字符還不夠,就讀入下一個字符,組成兩個字符的字符串。如果還沒有發現匹配,惰性量詞繼續從字符串中添加字符直到發現一個匹配或者整個字符串都檢查過也沒有匹配。惰性量詞和貪婪量詞的工作方式恰好相反。

    支配量詞:

只嘗試匹配整個字符串。如果整個字符串不能產生匹配,不做進一步嘗試。

    貪婪量詞   惰性量詞    支配量詞                      描述
    -------------------------------------------------------------------------------------
      ?             ??             ?+                      可以出現0次或1次,但至多出現1次
      *             *?               *+                      可以出現任意次,也可以不出現
      +             +?              ++                    出現1次或多次,但至少出現1次
      {n}          {n}?           {n}+                   一定出現n次
      {n,m}     {n,m}?       {n,m}+               至少出現n次,但至多不能超過m次
      {n,}         {n,}?          {n,}+                 可以出現任意次,但至少出現n次

    例如:我們要從字符串abbbaabbbaaabbb1234中獲得abbb,aabbb,aaabbb的匹配

    1、貪婪量詞

1        var regexp = /.*bbb/g;
2        var a = str.match(regexp);
3        alert(a.length);   //output:1
4        alert(a[0]);       //output:abbbaabbbaaabbb

    貪婪量詞的工作過程可以這樣表示:
      a)abbbaabbbaaabbb1234
      b)abbbaabbbaaabbb123
      c)abbbaabbbaaabbb12
      d)abbbaabbbaaabbb1
      e)abbbaabbbaaabbb //true
    可以看到,貪婪量詞在取得一次匹配後就會停止工作,雖然我們加了'g'(全局匹配)

    2、惰性量詞

1        var regexp = /.*?bbb/g;
2        var a = str.match(regexp);
3        alert(a.length);   //output:3
4        alert(a[0]);       //output:abbb
5        alert(a[1]);       //output:aabbb
6        alert(a[2]);       //output:aaabbb

    惰性量詞的工作過程可以這樣表示:
      a)a
      b)ab
      c)abb
      d)abbb //保存結果,並從下一個位置重新開始
  
      e)a 
      f)aa 
      g)aab
      h)aabb
      j)aabbb //保存結果,並從下一個位置重新開始
  
      e)a
      e)aa
      e)aaa
      e)aaab 
      e)aaabb 
      e)aaabbb  //保存結果,並從下一個位置重新開始

    由於JS是不支持支配量詞的,所以支配量詞我們只能用JAVA來演示:

1        String string = "abbbaabbbaaabbb1234";
2        Pattern p = Pattern.compile(".*+bbb");
3        Matcher m = p.matcher(string);
4        System.out.println(m.find());   //output:false
5

    因爲支配量詞采用一刀切的匹配方式,如:

    a)abbbaabbbaaabbb1234 //false


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章