EDIT Please try this:
public static void main(String[] args) {
String[] input = new String[]{"foo/*comment*/bar", "comm*/foobar/*ent", "comment*/foobar", "foobar/*comment"};
String pattern = "(?:/\\*[^\\*]+(?:\\*/)?|(?:/\\*)?[^\\*]+\\*/)";
List<String> listMatches = new ArrayList<String>();
String result = "";
for (String m : input) {
result = m.replaceAll(pattern, ""); //remove matches
listMatches.add(result); // append to list
System.out.println(result);
}
}
Output:
foobar
foobar
foobar
foobar
Here is the explanation of the regex:
(?: 1st non-capturing group starts
/\\* match /* literally
[^\\*]+ 1 or more times characters except *
(?: 2nd non-capturing group starts
\\*/ match */ literally
) 2nd non-capturing group ends
? match previous non-capturing group 0 or 1 time
| Or (signals next alternative)
(?: 3rd non-capturing group starts
/\\* match /* literally
) 3rd non-capturing group ends
? match previous non-capturing group 0 or 1 time
[^\\*]+ 1 or more times characters except *
\\*/ match */ one time
) 1st non-capturing group ends