Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java replace all square brackets in a string

I want to remove square brackets from a string, but I don't know how.

String str = "[Chrissman-@1]";
str = replaceAll("\\[\\]", "");

String[] temp = str.split("-@");
System.out.println("Nickname: " + temp[0] + " | Power: " + temp[1]);

But my result is: [Chrissman | 1] The square brackets doesn't get removed.

I tried using a different regex: "\\[.*?\\]", "\\[\\d+\\]" but the result is the same, the square brackets still attached on the string.

Edit:

I tried:

str.replaceAll("]", "");
str.replaceAll("[", "");

And now I'm getting:

Exception in thread "Thread-4" java.util.regex.PatternSyntaxException: Unclosed character class near index 0
[
^
    at java.util.regex.Pattern.error(Unknown Source)
    at java.util.regex.Pattern.clazz(Unknown Source)
    at java.util.regex.Pattern.sequence(Unknown Source)
    at java.util.regex.Pattern.expr(Unknown Source)
    at java.util.regex.Pattern.compile(Unknown Source)
    at java.util.regex.Pattern.<init>(Unknown Source)
    at java.util.regex.Pattern.compile(Unknown Source)
    at java.lang.String.replaceAll(Unknown Source)
like image 720
Zbarcea Christian Avatar asked Jan 21 '13 15:01

Zbarcea Christian


People also ask

How do you remove all brackets from a string?

replaceAll("\\D+", ""); it will remove all non digit characters from the string.

How do you replace parentheses in a string in Java?

Remove Parentheses From a String Using the replaceAll() Method. Yet another method to remove the parentheses from the Java String is to invoke the replaceAll() method. The replaceAll() method is a method of the String class.

How do you use brackets in a string in Java?

You will need to escape the '('. But whenever you put a single backslash inside a String literal, the compiler expects a character like 'n' (new line) , 't' (tab), a '"' (double quote), etc. So you can't escape opening or closing parentheses with a single backslash.


6 Answers

The replaceAll method is attempting to match the String literal [] which does not exist within the String try replacing these items separately.

String str = "[Chrissman-@1]";
str = str.replaceAll("\\[", "").replaceAll("\\]","");
like image 112
Kevin Bowersox Avatar answered Oct 05 '22 14:10

Kevin Bowersox


Your regex matches (and removes) only subsequent square brackets. Use this instead:

str = str.replaceAll("\\[|\\]", "");

If you only want to replace bracket pairs with content in between, you could use this:

str = str.replaceAll("\\[(.*?)\\]", "$1");
like image 21
Bergi Avatar answered Oct 05 '22 13:10

Bergi


You're currently trying to remove the exact string [] - two square brackets with nothing between them. Instead, you want to remove all [ and separately remove all ].

Personally I would avoid using replaceAll here as it introduces more confusion due to the regex part - I'd use:

String replaced = original.replace("[", "").replace("]", "");

Only use the methods which take regular expressions if you really want to do full pattern matching. When you just want to replace all occurrences of a fixed string, replace is simpler to read and understand.

(There are alternative approaches which use the regular expression form and really match patterns, but I think the above code is significantly simpler.)

like image 28
Jon Skeet Avatar answered Oct 05 '22 14:10

Jon Skeet


use regex [\\[\\]] -

String str = "[Chrissman-@1]";
String[] temp = str.replaceAll("[\\[\\]]", "").split("-@");
System.out.println("Nickname: " + temp[0] + " | Power: " + temp[1]);

output -

Nickname: Chrissman | Power: 1
like image 43
Subhrajyoti Majumder Avatar answered Oct 05 '22 14:10

Subhrajyoti Majumder


You may also do it like this:

String data = "[yourdata]";
String regex = "\\[|\\]";
data  = data .replaceAll(regex, "");
System.out.println(data);
like image 21
Zar E Ahmer Avatar answered Oct 05 '22 13:10

Zar E Ahmer


I use this regex to replace every string inside brackets:

var= var.replaceAll("\\[\\w+", "[*").replaceAll("\\]", "]");
like image 30
gOliveiraC Avatar answered Oct 05 '22 14:10

gOliveiraC