如何在java正则表达式模式中将"not"与更多字符匹配?

Koe*_*err 3 java regex

在java正则表达式中,用于[^x]将"not"与一个char匹配.

我想知道,如何匹配更多的字符不?

我使用[^789],这是不对的.

    String text="aa(123)bb(456)cc(789)dd(78)";
    text=text.replaceAll("\\([^789].*?\\)","");

    System.out.println(text);
Run Code Online (Sandbox Code Playgroud)

我想得到的结果是:

aabbcc(789)dd
Run Code Online (Sandbox Code Playgroud)

如何修复我的正则表达式?

非常感谢 :)

Mar*_*ers 7

您可以使用否定前瞻:

"\\((?!789\\)).*?\\)"
Run Code Online (Sandbox Code Playgroud)

说明:

\\(     Match a literal open parenthesis "("
(?!     Start negative lookahead
789\\)  Match literal "789)"
)       End lookahead
.*?     Match any characters (non-greedy)
\\)     Match a literal close parenthesis ")"

如果负前瞻内的模式匹配则负向前瞻不匹配.