Reputation: 1021
I need to remove everything between [ ]
.
Example:
My Input : ab[cd]e
Expected Output : abe
.
I tried using \[
and \]
, but it is reported as an illegal escape sequence.
Can anyone please help me with this.
P.S.: I am using Java 1.7.
Upvotes: 14
Views: 23379
Reputation: 91349
Use String#replaceAll
:
String s = "ab[cd]e";
s = s.replaceAll("\\[.*?\\]", ""); // abe
This will replace [
, ]
, and everything in between with an empty string.
Upvotes: 19