2016-03-03 87 views
0

我正在嘗試學習Java正則表達式。我想將幾個捕獲組(即j(a(va)))與另一個字符串(即this is java. this is ava, this is va)匹配。我期待的輸出爲:如何匹配多個捕獲組,但結果不如預期

I found the text "java" starting at index 8 and ending at index 12. 
I found the text "ava" starting at index 21 and ending at index 24.  
I found the text "va" starting at index 34 and ending at index 36. 
Number of group: 2 

但是,IDE而只輸出:

I found the text "java" starting at index 8 and ending at index 12. 
Number of group: 2 

爲什麼會出現這種情況?有什麼我失蹤?

原始代碼:

BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); 
System.out.println("\nEnter your regex:"); 

     Pattern pattern 
       = Pattern.compile(br.readLine()); 

     System.out.println("\nEnter input string to search:"); 
     Matcher matcher 
       = pattern.matcher(br.readLine()); 

     boolean found = false; 
     while (matcher.find()) { 
      System.out.format("I found the text" 
        + " \"%s\" starting at " 
        + "index %d and ending at index %d.%n", 
        matcher.group(), 
        matcher.start(), 
        matcher.end()); 
      found = true; 
      System.out.println("Number of group: " + matcher.groupCount()); 
     } 
     if (!found) { 
      System.out.println("No match found."); 
     } 

運行上面的代碼後,我已經進入了下輸入:

Enter your regex: 
j(a(va)) 

Enter input string to search: 
this is java. this is ava, this is va 

而且IDE輸出:

I found the text "java" starting at index 8 and ending at index 12. 
Number of group: 2 
+1

嘗試使用https://regex101.com/ –

+1

我想你誤會什麼捕獲組辦。它們不會使正則表達式的其他部分成爲可選項,所以您的正則表達式只匹配整個字符串'java'。 – Barmar

+1

請不要發佈從System.in'讀取的問題,並對結果進行一些處理,因爲這意味着您可以a)輕鬆地調試代碼以從System.in中讀取或從硬編碼中識別錯誤字符串。在這兩種情況下,這意味着代碼不是最簡單的例子,並且/或者錯誤的來源可以很容易地縮小。這也意味着更多的工作來重現問題。 – fabian

回答

1

你的正則表達式僅匹配整個字符串java,它不匹配avava。當它匹配java時,它會將捕獲組1設置爲ava並將組2捕獲到va,但它們與它們自己的字符串不匹配。這將產生你想要的結果的正則表達式是:

j?(a?(va)) 

?,使前項可選的,所以它會匹配以後的項目沒有這些前綴。

DEMO

+0

非常感謝您的幫助!真的很感激它! – Thor

1

您需要的正則表達式(j?(a?(va)))

Pattern p = Pattern.compile("(j?(a?(va)))"); 
Matcher m = p.matcher("this is java. this is ava, this is va"); 

while(m.find()) 
{ 
    String group = m.group(); 
    int start = m.start(); 
    int end = m.end(); 
    System.out.format("I found the text" 
        + " \"%s\" starting at " 
       + "index %d and ending at index %d.%n", 
        group, 
        start, 
        end); 



} 

你可以看到演示here