2014-01-08 73 views
4

這兩個正則表達式的含義是什麼?Java正則表達式中的混淆

.*? and .+? 

其實我明白用法這些量詞即

'.' -> Any character 
'*' -> 0 or more times 
'+' -> once or more times 
'?' -> 0 or 1 

事實上,我從字面上困惑!關於使用.*? and .+?。任何人都可以通過適當的例子顯示這些情況。

而且找你最歡迎大家分享呈現有用的例子做法很方便。 在此先感謝。

+2

您是否試過閱讀文檔?如果你有,而且事情還不清楚,請說明具體是什麼讓你感到困惑。 – NPE

+0

主要是:'*' - >表示0個字符或更多,'+' - >表示1個字符或更多,是什麼使你困惑? –

+2

公平地看待OP,是棘手的一點。 – Bathsheba

回答

7

要打破我們:

. - Any character 
* - Any number of times 
? - That is consumed reluctantly 

. - Any character 
+ - At least once 
? - That is consumed reluctantly 

一個不情願的或「非貪婪」數量(在「?」)匹配儘可能少,以便找到一個匹配。可以發現更多深入瞭解分類器(貪婪,不情願和佔有慾)here

+3

如果你解釋「不情願」,值得讚揚。 – Bathsheba

+0

@Bathsheba好點,回答更新...甚至解釋「reuluctantly」 –

+0

非常感謝!你能否使用上面提到的正則表達式來幫助Text Target。 – puru

3

.*?.+?是不情願的量詞。

他們開始在輸入字符串的開始,然後在尋找比賽時間勉強吃一個字符。他們嘗試的最後一件事是整個輸入字符串。

考慮代碼:

 String lines="some"; 
     String REGEX=".+?"; 
     Pattern pattern=Pattern.compile(REGEX); 
     Matcher matcher =pattern.matcher(lines); 
     while(matcher.find()){ 
      String result=matcher.group(); 
      System.out.println("RESULT of .+? : "+result); 
      System.out.println("RESULT LENGTH : "+result.length()); 
     } 
     System.out.println(lines); 
     String REGEX1=".*?"; 
     Pattern pattern1=Pattern.compile(REGEX1); 
     Matcher matcher1 =pattern1.matcher(lines); 
     while(matcher1.find()){ 
      int start=matcher1.start() ; 
      int end=matcher1.end() ; 
      String result=matcher1.group(); 
      System.out.println("RESULT of .*? : "+result); 
      System.out.println("RESULT LENGTH : "+result.length() +" , start "+ start+" end :"+end); 
     } 

OUTPUT:

RESULT of .+? : s 
RESULT LENGTH : 1 
RESULT of .+? : o 
RESULT LENGTH : 1 
RESULT of .+? : m 
RESULT LENGTH : 1 
RESULT of .+? : e 
RESULT LENGTH : 1 
some 
RESULT of .*? : 
RESULT LENGTH : 0 , start 0 end :0 
RESULT of .*? : 
RESULT LENGTH : 0 , start 1 end :1 
RESULT of .*? : 
RESULT LENGTH : 0 , start 2 end :2 
RESULT of .*? : 
RESULT LENGTH : 0 , start 3 end :3 
RESULT of .*? : 
RESULT LENGTH : 0 , start 4 end :4 

.+?將試圖找到在每個字符匹配,它(長度1)相匹配。

.*?將試圖找到在每個字符或什麼都不匹配。它匹配每個字符的空字符串。

+0

非常感謝。但我想(。*)會帶來羣體。所以在我的情況下,就像。*?和。+ ?.你能用相關的例子來證明這兩者嗎? – puru

+0

我更新了我的答案。 –

+1

非常好的例子。非常感謝... – puru

2

爲了說明,請考慮輸入字符串xfooxxxxxxfoo。

Enter your regex: .*foo // greedy quantifier 
Enter input string to search: xfooxxxxxxfoo 
I found the text "xfooxxxxxxfoo" starting at index 0 and ending at index 13. 

Enter your regex: .*?foo // reluctant quantifier 
Enter input string to search: xfooxxxxxxfoo 
I found the text "xfoo" starting at index 0 and ending at index 4. 
I found the text "xxxxxxfoo" starting at index 4 and ending at index 13. 

Enter your regex: .*+foo // possessive quantifier 
Enter input string to search: xfooxxxxxxfoo 
No match found. 

第一個例子使用貪婪量詞。*找到「什麼」,零次或多次,後加字母「F」「O」「O」。由於量詞是貪婪的,表達式的。*部分首先會吃掉整個輸入字符串。此時,整體表達不能成功,因爲最後三個字母(「f」「o」「o」)已被使用。所以匹配器一次只能退回一個字母,直到「foo」的最右側出現反芻,此時匹配成功並且搜索結束。

第二個例子,但是,不願意,所以它開始由第一消費「一無所有」。因爲「foo」沒有出現在字符串的開頭,所以它不得不吞下第一個字母(一個「x」),它會在0和4處觸發第一個匹配。我們的測試工具會繼續這個過程,直到輸入字符串爲止累。它在4和13找到另一個匹配。

第三個示例未能找到匹配,因爲量詞是所有格。在這種情況下,整個輸入字符串被。* +消耗掉,在表達式結尾處沒有任何結果滿足「foo」。使用佔有量詞來處理你想要抓住所有東西而又不退縮的情況;在沒有立即找到匹配的情況下,它將勝過等價的貪婪量詞。

您可以在鏈接中找到它http://docs.oracle.com/javase/tutorial/essential/regex/quant.html