COMMENTS 指定使用注釋和忽略空白,也就是".*a"==". *a #this is comments"我想這個 * 在正則表達式很大,而且是在文件中輸入時比較有用,平時我看也用不上。 * * MULTILINE In multiline mode the expressions ^ and $ match just after * or just before, respectively, a line terminator or the end of the * input sequence. By default these expressions only match at the beginning * and the end of the entire input sequence * 指定使用多行匹配模式,在默認模式下,^和$分別只匹配一個輸入的開始和結(jié)束。 * 在這種模式下,^和$ 除了匹配整個輸入的開始和結(jié)束外還匹配一個line terminator的后邊和 * 前邊(不是前邊和后邊,就是說^匹配line terminator的后邊$匹配line terminator的前邊。 * * DOATALL 如指定了這個模式則"."可匹配任何字符包括line terminator * * UNIX_LINES 指定這個模式時只有\(zhòng)n被認為是line terminator而\r和\r\n都不是 * * 其他的我一時想不起來了,在具體介紹時再說吧。 * /p> */ public class TestReg2 {
//演示appendXXX的用法 System.out.println("=================test append===================="); Pattern p4 = Pattern.compile("cat"); Matcher m4 = p4.matcher("one cat two cats in the yard"); StringBuffer sb = new StringBuffer(); boolean result = m4.find(); int i=0; System.out.println("one cat two cats in the yard"); while(result) {m4.appendReplacement(sb, "dog"); System.out.println(m4.group()); System.out.println("第"+i+++"次:"+sb.toString()); result = m4.find(); } System.out.println(sb.toString()); m4.appendTail(sb); System.out.println(sb.toString());
//test COMMENTS System.out.println("test COMMENTS"); Pattern p11=Pattern.compile(" a a #ccc",Pattern.COMMENTS); Matcher m11=p11.matcher("aa a a #ccc"); System.out.println(m11.find()); System.out.println(m11.find()); System.out.println("test COMMENTS"); Pattern p12 = Pattern.compile("(?x) a a #ccc"); Matcher m12 = p12.matcher("aa a a #ccc"); System.out.println(m12.find()); System.out.println(m12.find());
//注意下面三個例子體會Greedy,Reluctant and Possessive Quantifiers的不同 Pattern ppp=Pattern.compile(".*foo"); Matcher mmm=ppp.matcher("xfooxxxxxxfoo"); /** * Greedy quantifiers X? X, once or not at all X* X, zero or more times X+ X, one or more times X{n} X, exactly n times X(n,} X, at least n times X{n,m} X, at least n but not more than m times Greedy quantifiers是最常用的一種,如上,它的匹配方式是先匹配盡可能多的字符,當(dāng) 這樣造成整個表達式整體不能匹配時就退一個字符再試比如: .*foo與xfooxxxxxxfoo的匹配過程,.*先與整個輸入匹配,發(fā)現(xiàn)這樣不行,整個串不能匹配 * 于是退最后一個字符"o"再試,還不行,再退直到把foo都退出才發(fā)現(xiàn)匹配于是結(jié)束。因為這個過程 * 總是先從最大匹配開始到找到一個匹配,所以.*與之匹配的總是一個最大的,這個特點和資本家相似 * 故名貪婪的 */ boolean isEnd=false; int k=0; System.out.println("=========="); System.out.println("xfooxxxxxxfoo"); while(isEnd==false) try{ System.out.println("the:"+k++); System.out.println(mmm.find()); System.out.println(mmm.end()); }catch(Exception e){ isEnd=true; } isEnd=false; Pattern ppp1=Pattern.compile(".*?foo"); Matcher mmm1=ppp1.matcher("xfooxxxxxxfoo"); /** * Reluctant quantifiers X?? X, once or not at all X*? X, zero or more times X+? X, one or more times X{n}? X, exactly n times X(n,}? X, at least n times X{n,m}? X, at least n but not more than m times Reluctant quantifiers的匹配方式正好相反,它總是先從最小匹配開始,如果這時導(dǎo)致 整個串匹配失敗則再吃進一個字符再試,如: .*?foo與xfooxxxxxxfoo的匹配過程,首先,.*與空串匹配,這時整個串匹配失敗,于是 * 再吃一個x,這時發(fā)現(xiàn)整個串匹配成功,當(dāng)再調(diào)用find時從上次匹配結(jié)束時開始找,先吃一個 * 空串,不行,再吃一個x,不行,……直到把中間所有x都吃掉才發(fā)現(xiàn)匹配成功。這種方式總 * 是從最小匹配開始所以它能找到最多次數(shù)的匹配,但第一匹配都是最小的。它的行為有點象雇傭 * 工人,總是盡可能少的于活,故名勉強的。 */ k=0; System.out.println("?????????????????????"); System.out.println("xfooxxxxxxfoo"); while(isEnd==false) try{ System.out.println("the:"+k++); System.out.println(mmm1.find()); System.out.println(mmm1.end()); }catch(Exception e){ isEnd=true; } isEnd=false; Pattern pp2=Pattern.compile(".*+foo"); Matcher mm2=pp2.matcher("xfooxxxxxxfoo"); /** * Possessive quantifiers X?+ X, once or not at all X*+ X, zero or more times X++ X, one or more times X{n}+ X, exactly n times X(n,}+ X, at least n times X{n,m}+ X, at least n but not more than m times Possessive quantifiers 這種匹配方式與Greedy方式相似,所不同的是它不夠聰明,當(dāng) 它一口吃掉所有可以吃的字符時發(fā)現(xiàn)不匹配則認為整個串都不匹配,它不會試著吐出幾個。它的行 為和大地主相似,貪婪但是愚蠢,所以名曰強占的。 */