如何在java正则表达式中转义美元和大括号(即${title})?
即你是怎么做到的?
String string = "Sample string with ${title} to be inserted.";
string.replaceAll("${title}", title);
以下所有情况都会导致错误:
string.replaceAll("\\${title}", title);
string.replaceAll("\\\\${title}", title);
string.replaceAll("\\\\$\\{title\\}", title);
而且,似乎没有任何效果,这一切都会导致这样的错误:
java.util.regex.PatternSyntaxException: Illegal repetition near index 4 \\$\\{title\\}
at java.util.regex.Pattern.error(Pattern.java:1713)
at java.util.regex.Pattern.closure(Pattern.java:2775)
at java.util.regex.Pattern.sequence(Pattern.java:1889)
at java.util.regex.Pattern.expr(Pattern.java:1752)