通过匹配给定字符串中的模式获取字符串数组

问题描述:

我有一个模式@@{}并给定了一个字符串,我需要找出所有大括号之间进入的字符串。通过匹配给定字符串中的模式获取字符串数组

例子: 如果我的字符串是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

变化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