javaRegex——Greedy、Reluctant和Possessive,javaRegex

來源:互聯網
上載者:User

javaRegex——Greedy、Reluctant和Possessive,javaRegex

數量詞
 Greedy  Reluctant  Possessive 匹配
 X? X??  X?+  X,一次或一次也沒有
  X* X*?   X*+  X,零次或多次
  X+ X+?   X++  X,一次或多次
  X{n}   X{n}?  X{n}+  X,恰好 n 次
  X{n,}  X{n,}?   X{n,}+  X,至少 n 次
 X{n,m}  X{n,m}?   X{n,m}+   X,至少 n 次,但是不超過 m 次

Greedy:貪婪的;Reluctant:勉強的;Possessive :獨佔的。

測試1:

package jichu;import java.util.regex.Matcher;import java.util.regex.Pattern;public class MainClass {    public static void main(String[] args) {        Matcher m1 = Pattern.compile("\\w+").matcher("ababa");// 貪婪的        Matcher m2 = Pattern.compile("\\w+?").matcher("ababa");// 勉強的        Matcher m3 = Pattern.compile("\\w++").matcher("ababa");// 獨佔的        System.out.println(piPei(m1));        System.out.println(piPei(m2));        System.out.println(piPei(m3));    }    public static String piPei(Matcher m) {        StringBuffer s = new StringBuffer();        int i = 0;        while (m.find()) {            s.append("{匹配子串" + ++i + ":" + m.group() + ";");            s.append("開始位置:" + m.start() + ";");            s.append("結束位置:" + m.end() + ";}");        }        if (s.length() == 0) {            s.append("沒有匹配到!");        }        s.insert(0, "(模式" + m.pattern().pattern() + "):");        return s.toString();    }}

列印:

(模式\w+):{匹配子串1:ababa;開始位置:0;結束位置:5;}(模式\w+?):{匹配子串1:a;開始位置:0;結束位置:1;}{匹配子串2:b;開始位置:1;結束位置:2;}{匹配子串3:a;開始位置:2;結束位置:3;}{匹配子串4:b;開始位置:3;結束位置:4;}{匹配子串5:a;開始位置:4;結束位置:5;}(模式\w++):{匹配子串1:ababa;開始位置:0;結束位置:5;}

從測試1中可知:

1、對於貪婪的,會一次性匹配所有的字元;

2、對於勉強的,會從左至右一個一個的匹配;

3、對於獨佔的,與貪婪的一樣也是一次性匹配所有的字元;

測試2:(在測試1的基礎上修改main方法)

    public static void main(String[] args) {        Matcher m1 = Pattern.compile("\\w+b").matcher("ababa");// 貪婪的        Matcher m2 = Pattern.compile("\\w+?b").matcher("ababa");// 勉強的        Matcher m3 = Pattern.compile("\\w++b").matcher("ababa");// 獨佔的        System.out.println(piPei(m1));        System.out.println(piPei(m2));        System.out.println(piPei(m3));    }

列印:

(模式\w+b):{匹配子串1:abab;開始位置:0;結束位置:4;}(模式\w+?b):{匹配子串1:ab;開始位置:0;結束位置:2;}{匹配子串2:ab;開始位置:2;結束位置:4;}(模式\w++b):沒有匹配到!

從測試1、2中可知:

1、對於貪婪的,'\w+'已經一次性匹配了所有的字元;當模式後加'b'後,此時不匹配,然後回溯1個字元,匹配成功。

2、對於勉強的,從左至右匹配,匹配出兩個子串。

3、對於獨佔的,'\w++'已經一次性匹配了所有的字元;當模式後加'b'後,此時不匹配,與貪婪的不一樣的是它不會回溯,所以匹配失敗。

總結

1、Greedy數量詞為“貪婪的”,如名字一樣,多吃多佔,它會儘可能多的匹配字元,會回溯。

2、Reluctant數量詞為“勉強的”,奉行夠用主義,它會儘可能少的匹配字元。

3、Possessive數量詞為“獨佔的”,它會如Greedy一樣儘可能多的匹配字元,但是它不會回溯。

更多與Regex相關內容:

java正則規則表

java之Pattern類詳解

java之Matcher類詳解

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.