我的输入如下:
Input: 6jVYY3Xnqt<>:"/\|?*GjznpnRQSb
testInput = testInput.replaceAll("[<>:/\\\"|?*]", "-");
output: 6jVYY3Xnqt----\---GjznpnRQSb
但如果我这样做:
testInput = testInput.replaceAll("[<>:/\"|?*]", "-");
testInput = testInput.replace("\\", "-");
output: 6jVYY3Xnqt--------GjznpnRQSb
这是 java 7 中的错误吗?为什么 replaceAll
不采用 \
字符?
最佳答案
您需要对正则表达式中的反斜杠进行两次转义,一次用于字符串文字,一次用于正则表达式:
testInput= testInput.replaceAll("[<>:/\\\\\"|?*]", "-");
// ^^^^
// Represents one backslash
关于java - java中replaceAll方法的特殊问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27286030/