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

时间:2021-01-22 14:09:41

I have a pattern @@{} and given a string I need to find out all the strings coming in between the curly braces.

我有一个模式@@ {}并给出一个字符串,我需要找出大括号之间的所有字符串。

Example : If my string is Hi This is @@{first} and second is @@{second} along with third @@{third} string

示例:如果我的字符串为Hi这是@@ {first},第二个是@@ {second}以及第三个@@ {third}字符串

The output I expect is a string array consisting of elements:

我期望的输出是一个由元素组成的字符串数组:

first   
second  
third

My Java code for this goes as :

我的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());   
}

But the output which I am getting is

但我得到的输出是

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

Please guide me how to get the desired output and what mistake I am doing

请指导我如何获得所需的输出以及我正在做的错误

1 个解决方案

#1


7  

Change match.group() into match.group(1). Also, @ needs no escaping.

将match.group()更改为match.group(1)。此外,@不需要逃避。

#1


7  

Change match.group() into match.group(1). Also, @ needs no escaping.

将match.group()更改为match.group(1)。此外,@不需要逃避。