java regular expression to extract content within square brackets
Solution 1:
A bit more concise:
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));
}
Solution 2:
You should use a positive lookahead and lookbehind:
(?<=\[)([^\]]+)(?=\])
- (?<=[) Matches everything followed by [
- ([^]]+) Matches any string not containing ]
- (?=]) Matches everything before ]