【发布时间】:2012-06-14 21:32:24
【问题描述】:
我试图在 Java 中转义 RegExp 元字符。以下是我想要的:
INPUT STRING: "This is $ test"
OUTPUT STRING: "This is \$ test"
这是我目前正在做的,但它不起作用:
String inputStr= "This is $ test";
inputStr = inputStr.replaceAll("$","\\$");
但我得到了错误的输出:
"This is $ test$"
【问题讨论】:
-
只需使用
replace代替replaceAll。它会起作用,而且会更有效率。
标签: java regex string escaping