Remove everything in parentheses java using regex
As mentionend by by Jelvis, ".*" selects everything and converts "(ab) ok (cd)" to ""
The version below works in these cases "(ab) ok (cd)" -> "ok", by selecting everything except the closing parenthesis and removing the whitespaces.
test = test.replaceAll("\\s*\\([^\\)]*\\)\\s*", " ");
Strings are immutable. You have to do this:
name = name.replaceAll("\\(.*\\)", "");
Edit: Also, since the .*
is greedy, it will kill as much as it can. So "(abc)something(def)"
will be turned into ""
.