18

I want to remove all type of brackets character (example: [],(),{}) in string by using java.

I tried using this code:

String test = "watching tv (at home)"; 
test = test.replaceAll("(","");
test = test.replaceAll(")","");

But it's not working, help me please.

  • You have to add *\\* before *(* Try this... String test = "watching tv (at home)"; test = test.replaceAll("\\(","").replaceAll("\\)",""); – Akshay Sharma Jul 21 '17 at 06:38

8 Answers8

37

The first argument of replaceAll takes a regular expression.

All the brackets have meaning in regex: Brackets are used in regex to reference capturing groups, square brackets are used for character class & braces are used for matched character occurrence. Therefore they all need to be escaped...However here the characters can simply be enclosed in a character class with just escaping required for square brackets

test = test.replaceAll("[\\[\\](){}]","");
Reimeus
  • 158,255
  • 15
  • 216
  • 276
27

To remove all punctuation marks that include all brackets, braces and sq. brackets ... as per the question is:

String test = "watching tv (at home)"; 
test = test.replaceAll("\\p{P}","");
StackFlowed
  • 6,664
  • 1
  • 29
  • 45
  • 8
    @MuhammadHaryadiFutra Be careful this solution removes also symbols like `.`, `,`, `;`, etc. – jeojavi Sep 15 '14 at 17:08
  • this just don't remove brackets but also other characters like "/" and "-". etc (16180856-04-20170407-ad/wav/D104) changed to 161808560420170407adwavD104 – Adnan Ali Jun 07 '17 at 11:55
  • @AdnanAli please read the answer correctly if you miss that then read the comment above your :) – StackFlowed Jun 08 '17 at 18:09
  • This removes other symbols too. So if someone uses this without knowing that, it might cause major issues – Ramanan Durairaj Sep 02 '20 at 16:40
6

The first argument passed to the replaceAll() method should be a regular expression. If you want to match those literal bracket characters you need to escape \\(, \\) them.

You could use the following to remove bracket characters. Unicode property \p{Ps} will match any kind of opening bracket and Unicode property \p{Pe} matches any kind of closing bracket.

String test = "watching tv (at home) or [at school] or {at work}()[]{}";
test = test.replaceAll("[\\p{Ps}\\p{Pe}]", "");
System.out.println(test); //=> "watching tv at home or at school or at work"
hwnd
  • 69,796
  • 4
  • 95
  • 132
3

You need to escape the bracket as it will be treated as part of a regex

String test = "watching tv (at home)"; 
test = test.replaceAll("\\(","");
test = test.replaceAll("\\)","");

Also to remove all brackets try

String test = "watching tv (at home)"; 
test = test.replaceAll("[\\(\\)\\[\\]\\{\\}]","");
AbstractChaos
  • 4,211
  • 1
  • 19
  • 28
  • 1
    `"\("` will cause a compilation error as it is not a valid escape character. `"\\("` is what is needed – zero_dev Sep 15 '14 at 16:55
3

You can use String.replace instead of String.replaceAll for better performance, as it searches for the exact sequence and does not need regular expressions.

String test = "watching tv (at home)"; 
test = test.replace("(", " ");
test = test.replace(")", " ");
test = test.replace("[", " ");
test = test.replace("]", " ");
test = test.replace("{", " ");
test = test.replace("}", " ");

If you are working with texts I recommend you to replace the brackets with an empty space to avoid words joining together: watching tv(at home) -> watching tvat home

jeojavi
  • 876
  • 1
  • 6
  • 15
0

subject =StringUtils.substringBetween(subject, "[", "]")

0

String list= '[{121223123123123123}]';

String accountlist= list.replaceAll("[\[\]]","");

In this case I am removing [ ] from a string.

Output : String list= '{121223123123123123}';

Lakshay Sharma
  • 1,347
  • 12
  • 13
0

For removing Brackets and other punctuations from the String you can use

String test = "watching tv (at home)"; 
test = test.replaceAll("\\p{P}","");

But if are you are intending for numbers only Phone Number, the below will be useful:

test = test.replaceAll("\\D+", "");

it will remove all non digit characters from the string.