用于提取方括号内内容的java正则表达式

用于提取方括号内内容的java正则表达式,java,regex,Java,Regex,输入行在下面 Item(s): [item1.test],[item2.qa],[item3.production] 你能帮我写一个Java正则表达式来解压吗 item1.test,item2.qa,item3.production 从上面的输入行?我将在修剪前面或后面的垃圾后拆分: String s = "Item(s): [item1.test], [item2.qa],[item3.production] "; String r1 = "(^.*?\\[|\\]\\s*$)", r2

输入行在下面

Item(s): [item1.test],[item2.qa],[item3.production]
你能帮我写一个Java正则表达式来解压吗

item1.test,item2.qa,item3.production

从上面的输入行?

我将在修剪前面或后面的垃圾后拆分:

String s = "Item(s): [item1.test], [item2.qa],[item3.production] ";
String r1 = "(^.*?\\[|\\]\\s*$)", r2 = "\\]\\s*,\\s*\\[";
String[] ss = s.replaceAll(r1,"").split(r2);
System.out.println(Arrays.asList(ss));
// [item1.test, item2.qa, item3.production]
更简洁一点:

String in = "Item(s): [item1.test],[item2.qa],[item3.production]";

Pattern p = Pattern.compile("\\[(.*?)\\]");
Matcher m = p.matcher(in);

while(m.find()) {
    System.out.println(m.group(1));
}

您应该使用积极的向前看和向后看:

(?<=\[)([^\]]+)(?=\])

(?@Stephan Kristyn:在Mac OS X 10.6.7.0上的Java 1.6上对我有效,但我怎么能得到相反的结果?我只想保留方括号内的内容我不理解你的问题-这正是这个regexp的功能。输入
项:[item1.test],[item2.qa],[item3.production]
它返回
item1.test
item3.production
您能解释一下模式的含义吗?谢谢