Java替换字符串中的所有方括号

时间:2022-12-07 21:43:03

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.

但我的结果是:[Chrissman | 1]方括号没有被移除。

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

我尝试使用不同的regex:“\(.)*?”“\”\“\”\“\”\“\”\“\”\“\“\”\“\”\“\”\“\“\”\“\“\”\“\“\”\“\“\”\“但结果是一样的,方括号仍然附在弦上。”

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)

6 个解决方案

#1


93  

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

replaceAll方法试图匹配字符串字面量[],字符串中不存在,尝试分别替换这些项。

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

#2


48  

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

您的regex只匹配(并删除)后续的方括号。取代它可使用:

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

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

如果您只想用中间的内容替换括号对,可以使用以下方法:

str = str.replaceAll("\\[(.*?)\\]", "$1");

#3


19  

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:

就我个人而言,我将避免在这里使用replaceAll,因为regex部分带来了更多的混乱——我将使用:

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.)

(有一些使用正则表达式形式和真正匹配模式的替代方法,但是我认为上面的代码要简单得多。)

#4


8  

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

#5


4  

You may also do it like this:

你也可以这样做:

String data = "[yourdata]";
String regex = "\\[|\\]";
data  = data .replaceAll(regex, "");
System.out.println(data);

#6


0  

Use this line:) String result = strCurBal.replaceAll("[(" what ever u need to remove ")]", ""); 

                String strCurBal = "(+)3428";
                Log.e(" before omit ", strCurBal);
                String result = strCurBal.replaceAll("[()]", "");
                Log.e(" Result Omit ", result);

                o/p :
                before omit : (+)3428
                Result Omit :  +3428


            String finalVal = result.replaceAll("[+]", "");
            Log.e("finalVal  ", finalVal);

            finalVal : 3428

#1


93  

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

replaceAll方法试图匹配字符串字面量[],字符串中不存在,尝试分别替换这些项。

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

#2


48  

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

您的regex只匹配(并删除)后续的方括号。取代它可使用:

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

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

如果您只想用中间的内容替换括号对,可以使用以下方法:

str = str.replaceAll("\\[(.*?)\\]", "$1");

#3


19  

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:

就我个人而言,我将避免在这里使用replaceAll,因为regex部分带来了更多的混乱——我将使用:

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.)

(有一些使用正则表达式形式和真正匹配模式的替代方法,但是我认为上面的代码要简单得多。)

#4


8  

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

#5


4  

You may also do it like this:

你也可以这样做:

String data = "[yourdata]";
String regex = "\\[|\\]";
data  = data .replaceAll(regex, "");
System.out.println(data);

#6


0  

Use this line:) String result = strCurBal.replaceAll("[(" what ever u need to remove ")]", ""); 

                String strCurBal = "(+)3428";
                Log.e(" before omit ", strCurBal);
                String result = strCurBal.replaceAll("[()]", "");
                Log.e(" Result Omit ", result);

                o/p :
                before omit : (+)3428
                Result Omit :  +3428


            String finalVal = result.replaceAll("[+]", "");
            Log.e("finalVal  ", finalVal);

            finalVal : 3428