Java 通过匹配给定字符串中的模式来获取字符串数组
我有一个模式Java 通过匹配给定字符串中的模式来获取字符串数组,java,regex,Java,Regex,我有一个模式@{},给定一个字符串,我需要找出花括号之间的所有字符串 Pattern p = Pattern.compile("\\@\\@\\{(.+?)\\}"); Matcher match = p.matcher("Hi This is @@{first} and second is @@{second} along" + "with third @@{third} string"); while(match.find()) {
@{}
,给定一个字符串,我需要找出花括号之间的所有字符串
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());
}
例如:
如果我的字符串是Hi,这是@first},第二个是@second},第三个是@second},第三个是@second}字符串
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
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());
}
我的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());
}
但我得到的结果是
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}
请指导我如何获得所需的输出以及我所犯的错误将
match.group()
更改为match.group(1)
。而且,@
不需要逃避。顺便说一句,看看你前面的问题。。。如果一个答案解决了你的问题,不要忘记勾选左边的绿色复选标记,将其标记为“已接受”。+1表示写得非常清楚的问题
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());
}