开发者

how to matching "not" with more chars in java regex pattern?

in java regex,use [^x] to matching "not" with one char.

i want to know,how to开发者_如何学C matching more chars not?

i using [^789],it's not right.

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

    System.out.println(text);

i want get result is:

aabbcc(789)dd

how to fix my regex pattern?

thanks a lot :)


You can use a negative lookahead:

"\\((?!789\\)).*?\\)"

Explanation:

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

If the pattern inside the negative lookahead matches then negative lookahead fails to match.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜