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

so_*_*_mv 31 java regex

输入行在下面

Item(s): [item1.test],[item2.qa],[item3.production]
Run Code Online (Sandbox Code Playgroud)

你能帮我写一个Java正则表达式来提取吗?

item1.test,item2.qa,item3.production
Run Code Online (Sandbox Code Playgroud)

从上面的输入线?

Jar*_*red 82

更简洁一点:

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));
}
Run Code Online (Sandbox Code Playgroud)

  • 您能解释一下该模式的含义吗?谢谢 (2认同)

gno*_*nom 8

你应该使用积极的前瞻和后视:

(?<=\[)([^\]]+)(?=\])
Run Code Online (Sandbox Code Playgroud)
  • (?<= []匹配所有后跟[
  • ([^]] +)匹配任何不包含的字符串]
  • (?=])匹配之前的一切]