为什么 String.replaceAll() 在 Java 8 和 Java 9 中的工作方式不同?

Why does String.replaceAll() work differently in Java 8 from Java 9?

为什么这段代码在 but o2 in 或以上输出02

"o2".replaceAll("([oO])([^[0-9-]])", "0")

很可能是由于 JDK-6609854 and JDK-8189343 报告了负面嵌套字符 类 处理(在您的示例中 [^[0-9-]])。此行为已在 9 和 10 中修复,但修复未反向移植到 8。Java 8 的错误解释为:

In Java, the negation does not apply to anything appearing in nested [brackets]

So [^c] does not match "c", as you would expect.

[^[c]] does match "c". Not what I would expect.

[[^c]] does not match "c"

The same holds true for ranges or property expressions - if they're inside brackets, a negation at an out level does not affect them.

[^a-z] is opposite from [^[a-z]]