Escape Java RegExp Metacharacters
问题 I'm trying to escape a RegExp metacharacter in Java. Below is what I want: INPUT STRING: "This is $ test" OUTPUT STRING: "This is \$ test" This is what I'm currently doing but it's not working: String inputStr= "This is $ test"; inputStr = inputStr.replaceAll("$","\\$"); But I'm getting wrong output: "This is $ test$" 回答1: You'll need: inputStr.replaceAll("\\$", "\\\\\\$"); The String to be replaced needs 2 backslashes because $ has a special meaning in the regexp. So $ must be escaped, to