以下是输入行
Item(s): [item1.test],[item2.qa],[item3.production]
您能帮我编写一个Java正则表达式来提取数据吗?
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));
}
您应该使用正向预查和反向预查:
(?<=\[)([^\]]+)(?=\])
Item(s): [item1.test],[item2.qa],[item3.production]
它返回
item1.test
item2.qa
item3.production
- gnom1gnomString 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]