假设括号不需要配对,例如((((5))应该变成(5),那么下面的代码就可以了:str = str.replaceAll("([()])\\1+", "$1");测试for (String str : new String[] { "(5)", "((5))", "((((5))))", "((((5))" }) { str = str.replaceAll("([()])\\1+", "$1"); System.out.println(str);}输出(5)(5)(5)(5)解释( Start capture group [()] Match a '(' or a ')'. In a character class, '(' and ')' has no special meaning, so they don't need to be escaped) End capture group, i.e. capture the matched '(' or ')'\1+ Match 1 or more of the text from capture group #1. As a Java string literal, the `\` was escaped (doubled)$1 Replace with the text from capture group #1另请参阅regex101.com以获取演示。