[英]How to replace a substring within double parentheses in Java?
I need to replace a substring within double parentheses with an empty one. 我需要用空括号替换双括号中的子串。 I use:
我用:
source = source.replaceAll("\\(.+?\\)", "");
The substrings within single parentheses are removed. 单个括号内的子串被删除。 Instead, when I try with a substring within double parentheses the entire substring is removed but the last ')' remains.
相反,当我尝试使用双括号内的子字符串时,整个子字符串被删除但最后一个')'仍然存在。
What's wrong here? 这有什么不对?
Thanks in advance. 提前致谢。
source.replaceAll("\\(\\([^\\(]*\\)\\)", "");
? source.replaceAll("\\(\\([^\\(]*\\)\\)", "");
;?
(not tested) (未测试)
The +? +? means that your match is going to take the least amount of characters possible, meaning that it will grab the inner parenthesized statement.
意味着你的匹配将占用尽可能少的字符,这意味着它将获取内部带括号的语句。
Try: 尝试:
source = source.replaceAll("\(?\(.+?\)\)?", "");
I've just wrapped your regex with \\(?
and \\)?
我刚用
\\(?
和\\)?
包装你的正则表达式\\)?
. 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.