输入行位于
之下Item(s): [item1.test],[item2.qa],[item3.production]
你能帮我写一个Java正则表达式来提取
吗?item1.test,item2.qa,item3.production
来自输入线?
答案 0 :(得分:78)
更简洁一点:
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));
}
答案 1 :(得分:8)
你应该使用积极的前瞻和后视:
(?<=\[)([^\]]+)(?=\])
答案 2 :(得分:1)
我会在修剪前面或后面的垃圾后拆分:
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]