2009-11-10 81 views
4

我有一個模式@@{}並給定了一個字符串,我需要找出所有大括號之間進入的字符串。通過匹配給定字符串中的模式獲取字符串數組

例子: 如果我的字符串是Hi This is @@{first} and second is @@{second} along with third @@{third} string

我期望的輸出是一個字符串數組組成元素:

first 
second 
third 

這個我的Java代碼爲:

Pattern p = Pattern.compile("\\@\\@\\{(.+?)\\}");  
Matcher match = p.matcher("Hi This is @@{first} and second is @@{second} along" + 
          "with third @@{third} string"); 
while(match.find()) { 
    System.out.println(match.group()); 
} 

但我得到的輸出是

@@{first} 
@@{second} 
@@{third} 

請指導我如何得到需要的結果和我在做什麼錯誤

+2

順便說一句,看看你以前的問題......如果一個答案解決了你的問題,不要忘記在左邊勾上綠色的選中標記,將其標記爲「接受」。 – Jonik 2009-11-10 07:51:20

+1

+1非常明確的書面問題 – 2009-11-10 08:54:31

回答

7

變化match.group()match.group(1)。另外,@不需要轉義。

+0

非常感謝Bart – Amit 2009-11-10 09:01:12

+0

不客氣Amit。 – 2009-11-10 09:07:13

+0

@不允許,請將此答案標記爲已接受。 – 2009-11-10 17:59:14