[英]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 |但我的结果是:[Chrissman | 1] The square brackets doesn't get removed.
1] 方括号不会被删除。
I tried using a different regex: "\\\\[.*?\\\\]"
, "\\\\[\\\\d+\\\\]"
but the result is the same, the square brackets still attached on the string.我尝试使用不同的正则表达式:
"\\\\[.*?\\\\]"
, "\\\\[\\\\d+\\\\]"
但结果是一样的,方括号仍然附在字符串上。
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)
The replaceAll method is attempting to match the String literal []
which does not exist within the String
try replacing these items separately.所述的replaceAll方法试图匹配字符串文字
[]
不将内存在String
尝试单独更换这些项目。
String str = "[Chrissman-@1]";
str = str.replaceAll("\\[", "").replaceAll("\\]","");
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");
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
,因为它会由于正则表达式部分而引入更多混乱 - 我会使用:
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.当您只想替换所有出现的固定字符串时,
replace
更易于阅读和理解。
(There are alternative approaches which use the regular expression form and really match patterns, but I think the above code is significantly simpler.) (有使用正则表达式形式并真正匹配模式的替代方法,但我认为上面的代码要简单得多。)
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
You may also do it like this:你也可以这样做:
String data = "[yourdata]";
String regex = "\\[|\\]";
data = data .replaceAll(regex, "");
System.out.println(data);
Use this line:) String result = strCurBal.replaceAll("[(" what ever u need to remove ")]", ""); String strCurBal = "(+)3428"; Log.e("Agilanbu before omit ", strCurBal); String result = strCurBal.replaceAll("[()]", ""); // () removing special characters from string Log.e("Agilanbu after omit ", result); o/p : Agilanbu before omit : (+)3428 Agilanbu after omit : +3428 String finalVal = result.replaceAll("[+]", ""); // + removing special characters from string Log.e("Agilanbu finalVal ", finalVal); o/p Agilanbu finalVal : 3428 String finalVal1 = result.replaceAll("[+]", "-"); // insert | append | replace the special characters from string Log.e("Agilanbu finalVal ", finalVal1); o/p Agilanbu finalVal : -3428 // replacing the + symbol to -
String str, str1;
Scanner sc = new Scanner(System.in);
System.out.print("Enter a String : ");
str = sc.nextLine();
str1 = str.replaceAll("[aeiouAEIOU]", "");
System.out.print(str1);
我使用这个正则表达式来替换括号内的每个字符串:
var= var.replaceAll("\\[\\w+", "[*").replaceAll("\\]", "]");
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.