Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我正在尝试不区分大小写的模式匹配。为了学习,我尝试了以下方法,发现很难分析正在发生的事情。
String x = "Hello"; String pattern = "(?i)"; System.out.println(x.replaceAll(pattern, "</code>"));
输出是
</code>H</code>e</code>l</code>l</code>o</code>
有人可以解释一下这种行为吗
使用(?i)bare相当于匹配case-insensitive空String
(?i)
case-insensitive
您需要在(?i)将不区分大小写的匹配应用于..
这就是为什么此模式匹配每个空字符串、、after each character和also before the first character,并将其替换为:-</code>
after each character
also before the first character
</code>
该模式与任何内容都不匹配,并且每个字符的两侧都没有任何内容:)
(嗯,实际上有很多空,但每个索引只能有一个匹配项。)