简体   繁体   English

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

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

Why does this code output 02 in but o2 in or above? 为什么这个代码在输出02但在或更高版本中输出o2

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

Most likely due to JDK-6609854 and JDK-8189343 which reported negative nested character classes handling (in your example [^[0-9-]] ). 很可能是由于JDK-6609854JDK-8189343报告了负嵌套字符类处理(在您的示例中[^[0-9-]] )。 This behavior was fixed in 9 and 10, but fix was not backported to 8. The bug for Java 8 is explained as: 此行为在9和10中得到修复,但修复程序未向后移植到8. Java 8的错误解释为:

In Java, the negation does not apply to anything appearing in nested [brackets] 在Java中,否定并不适用于任何出现在嵌套的[brackets]

So [^c] does not match "c", as you would expect. 所以[^c]与“c”不匹配,正如您所期望的那样。

[^[c]] does match "c". [^[c]]匹配“c”。 Not what I would expect. 不是我所期待的。

[[^c]] does not match "c" [[^c]]与“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. 对于范围或属性表达式也是如此 - 如果它们在括号内,则在out级别的否定不会影响它们。

[^az] is opposite from [^[az]] [^az][^[az]]相反

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM