replaceAll 似乎不适用于正则表达式

replaceAll does not seem to be working with regex

我想替换字符串中所有非字母数字或不属于用户输入的特殊字符列表的字符。

String pagePath = "/content/geo/en/tool";
String specialCharacters = "\:*?\"<>|#";
String fileName = pagePath.replaceAll("[^\p{IsAlphabetic}^\p{IsDigit}\:*?\"<>|#]", replacer); //This works fine o/p: ~content~geo~en~tool

String test2 = "\"[^\p{IsAlphabetic}^\p{IsDigit}" + specialCharacters + "]\"";
String fileName2 = pagePath.replaceAll(test2, replacer);

由于某种原因,fileName2 的值仍然存在,/content/geo/en/tool

有人可以帮我解决问题吗?

无需转义 test2:

中的引号
String test2 = "[^\p{IsAlphabetic}^\p{IsDigit}" + specialCharacters + "]";