gpt4 book ai didi

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

转载 作者:IT老高 更新时间:2023-10-28 13:53:46 24 4
gpt4 key购买 nike

为什么这段代码在中输出02o2或以上?

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

最佳答案

很可能是由于 JDK-6609854JDK-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]]

关于java - 为什么 String.replaceAll() 在 Java 8 和 Java 9 中的工作方式不同?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54946363/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com