jean
jean

Reputation: 3

How do I java regex find and replace

What's the regex for removing parentheses and everything between?

input  = "hello world (this is a test1) (test 2) hello";
output = "hello world   hello";

I tried:

str.replaceAll("//(.*?//)", "");

Upvotes: 0

Views: 63

Answers (1)

M A
M A

Reputation: 72844

You are using forward slash characters to escape the ( and ) special characters. You need to use backslashes:

String output = input.replaceAll("\\(.*?\\)", "");

Upvotes: 1

Related Questions