java 正则表达式替换括号之间的内容 ()
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5636048/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Regular expression to replace content between parentheses ()
提问by Praneel PIDIKITI
I tried this code:
我试过这个代码:
string.replaceAll("\(.*?)","");
But it returns null. What am I missing?
但它返回空值。我错过了什么?
回答by Mike Thomsen
Try:
尝试:
string.replaceAll("\(.*?\)","");
You didn't escape the second parenthesis and you didn't add an additional "\" to the first one.
您没有转义第二个括号,也没有在第一个括号中添加额外的“\”。
回答by ridgerunner
First, Do you wish to remove the parentheses along with their content? Although the title of the question indicates no, I am assuming that you do wish to remove the parentheses as well.
首先,您要删除括号及其内容吗?尽管问题的标题表明没有,但我假设您也确实希望删除括号。
Secondly, can the content between the parentheses contain nested matching parentheses? This solution assumes yes. Since the Java regex flavor does not support recursive expressions, the solution is to first craft a regex which matches the "innermost" set of parentheses, and then apply this regex in an iterative manner replacing them from the inside-out. Here is a tested Java program which correctly removes (possibly nested) parentheses and their contents:
其次,括号之间的内容是否可以包含嵌套匹配的括号?此解决方案假设是。由于 Java 正则表达式风格不支持递归表达式,因此解决方案是首先制作一个与“最内层”括号集匹配的正则表达式,然后以迭代方式应用此正则表达式,从内到外替换它们。这是一个经过测试的 Java 程序,它可以正确删除(可能是嵌套的)括号及其内容:
import java.util.regex.*;
public class TEST {
public static void main(String[] args) {
String s = "stuff1 (foo1(bar1)foo2) stuff2 (bar2) stuff3";
String re = "\([^()]*\)";
Pattern p = Pattern.compile(re);
Matcher m = p.matcher(s);
while (m.find()) {
s = m.replaceAll("");
m = p.matcher(s);
}
System.out.println(s);
}
}
Test Input:
测试输入:
"stuff1 (foo1(bar1)foo2) stuff2 (bar2) stuff3"
Test Output:
测试输出:
"stuff1 stuff2 stuff3"
Note that the lazy-dot-star solution will never work, because it fails to match the innermost set of parentheses when they are nested. (i.e. it erroneously matches: (foo1(bar1)
in the example above.) And this is a verycommonly made regex mistake: Never use the dot when there is a more precise expression!In this case, the contents between an "innermost" set of matching parentheses consists of any character that is not an opening or closing parentheses, (i.e. Use: [^()]*
instead of: .*?
).
请注意,lazy-dot-star 解决方案永远不会起作用,因为它在嵌套时无法匹配最里面的一组括号。(即它错误地匹配:(foo1(bar1)
在上面的示例中。)这是一个非常常见的正则表达式错误:当有更精确的表达式时,切勿使用点!在这种情况下,“最里面”的一组匹配括号之间的内容由不是左括号或右括号的任何字符组成,(即使用:[^()]*
而不是:).*?
。
回答by NPE
Try string.replaceAll("\\(.*?\\)","")
.
试试string.replaceAll("\\(.*?\\)","")
。
回答by Java Drinker
string.replaceAll("\\([^\\)]*\\)","");
This way you are saying match a bracket, then all non-closing bracket chars, and then a closing bracket. This is usually faster than reluctant or greedy .* matchers.
string.replaceAll("\\([^\\)]*\\)","");
这样你就说匹配一个括号,然后是所有非右括号字符,然后是一个右括号。这通常比不情愿或贪婪的 .* 匹配器更快。