I've used the following regex to try to remove parentheses and everything within them in a string called name
.
我已经使用以下正则表达式尝试在名为name的字符串中删除括号及其中的所有内容。
name.replaceAll("\\(.*\\)", "");
For some reason, this is leaving name unchanged. What am I doing wrong?
由于某种原因,这是保持名称不变。我究竟做错了什么?
5 个解决方案
#1
20
Strings are immutable. You have to do this:
字符串是不可变的。你必须这样做:
name = name.replaceAll("\\(.*\\)", "");
Edit: Also, since the .*
is greedy, it will kill as much as it can. So "(abc)(def)"
will be turned into ""
.
编辑:此外,由于。*是贪婪的,它会尽可能多地杀死它。所以“(abc)(def)”将变成“”。
#2
7
As mentionend by by Jelvis, ".*" selects everything and converts "(ab) ok (cd)" to ""
正如Jelvis所提到的,“。*”选择所有内容并将“(ab)ok(cd)”转换为“”
The version below works in these cases "(ab) ok (cd)" -> "ok", by selecting everything except the closing parenthesis and removing the whitespaces.
下面的版本适用于这些情况“(ab)ok(cd)” - >“ok”,通过选择除右括号以外的所有内容并删除空格。
test = test.replaceAll("\\s*\\([^\\)]*\\)\\s*", " ");
#3
2
String.replaceAll()
doesn't edit the original string, but returns the new one. So you need to do:
String.replaceAll()不会编辑原始字符串,但会返回新字符串。所以你需要这样做:
name = name.replaceAll("\\(.*\\)", "");
#4
2
If you read the Javadoc for String.replaceAll()
, you'll notice that it specifies that the resulting string is the return value.
如果您读取String.replaceAll()的Javadoc,您会注意到它指定结果字符串是返回值。
More generally, String
s are immutable in Java; they never change value.
更一般地说,字符串在Java中是不可变的;他们永远不会改变价值
#5
1
I'm using this function:
我正在使用这个功能:
public static String remove_parenthesis(String input_string, String parenthesis_symbol){
// removing parenthesis and everything inside them, works for (),[] and {}
if(parenthesis_symbol.contains("[]")){
return input_string.replaceAll("\\s*\\[[^\\]]*\\]\\s*", " ");
}else if(parenthesis_symbol.contains("{}")){
return input_string.replaceAll("\\s*\\{[^\\}]*\\}\\s*", " ");
}else{
return input_string.replaceAll("\\s*\\([^\\)]*\\)\\s*", " ");
}
}
You can call it like this:
你可以这样称呼它:
remove_parenthesis(g, "[]");
remove_parenthesis(g, "{}");
remove_parenthesis(g, "()");
#1
20
Strings are immutable. You have to do this:
字符串是不可变的。你必须这样做:
name = name.replaceAll("\\(.*\\)", "");
Edit: Also, since the .*
is greedy, it will kill as much as it can. So "(abc)(def)"
will be turned into ""
.
编辑:此外,由于。*是贪婪的,它会尽可能多地杀死它。所以“(abc)(def)”将变成“”。
#2
7
As mentionend by by Jelvis, ".*" selects everything and converts "(ab) ok (cd)" to ""
正如Jelvis所提到的,“。*”选择所有内容并将“(ab)ok(cd)”转换为“”
The version below works in these cases "(ab) ok (cd)" -> "ok", by selecting everything except the closing parenthesis and removing the whitespaces.
下面的版本适用于这些情况“(ab)ok(cd)” - >“ok”,通过选择除右括号以外的所有内容并删除空格。
test = test.replaceAll("\\s*\\([^\\)]*\\)\\s*", " ");
#3
2
String.replaceAll()
doesn't edit the original string, but returns the new one. So you need to do:
String.replaceAll()不会编辑原始字符串,但会返回新字符串。所以你需要这样做:
name = name.replaceAll("\\(.*\\)", "");
#4
2
If you read the Javadoc for String.replaceAll()
, you'll notice that it specifies that the resulting string is the return value.
如果您读取String.replaceAll()的Javadoc,您会注意到它指定结果字符串是返回值。
More generally, String
s are immutable in Java; they never change value.
更一般地说,字符串在Java中是不可变的;他们永远不会改变价值
#5
1
I'm using this function:
我正在使用这个功能:
public static String remove_parenthesis(String input_string, String parenthesis_symbol){
// removing parenthesis and everything inside them, works for (),[] and {}
if(parenthesis_symbol.contains("[]")){
return input_string.replaceAll("\\s*\\[[^\\]]*\\]\\s*", " ");
}else if(parenthesis_symbol.contains("{}")){
return input_string.replaceAll("\\s*\\{[^\\}]*\\}\\s*", " ");
}else{
return input_string.replaceAll("\\s*\\([^\\)]*\\)\\s*", " ");
}
}
You can call it like this:
你可以这样称呼它:
remove_parenthesis(g, "[]");
remove_parenthesis(g, "{}");
remove_parenthesis(g, "()");