Jian
Jian

Reputation: 25

Regex: How to remove a substring that is bounded by certain characters?

I'm not sure what the appropriate regex expression would be for this:

String s = "[Don't remove] Don't remove [Remove | Don't remove]";

I want to remove everything in between [ and | but not [ and ]. So the output is:

"[Don't remove] Don't remove Don't remove]"

I tried doing this,

s = s.replaceAll("\\[.*?\\|", "");

but I end up getting something like this.

"Don't remove]"

Now I'm at a lost. I'm still new to regular expressions and any help would be greatly appreciated. Thanks!

Upvotes: 2

Views: 87

Answers (1)

Wiktor Stribiżew
Wiktor Stribiżew

Reputation: 627302

Use a ngated character class [^\[|]* that will not allow matching any other [ and | in between [ and |:

String s = "[Don't remove] Don't remove [Remove | Don't remove]";
s = s.replaceAll("\\[[^\\[|]*\\|", "");
System.out.println(s); // => [Don't remove] Don't remove  Don't remove]

See a regex demo and an online Java demo.

Details

  • \\[ - a literal [
  • [^\\[|]* - a negated character class matching any 0+ chars other than a [ and |
  • \\| - a literal | symbol.

Upvotes: 3

Related Questions