How to escape text for regular expression in Java
Since Java 1.5, yes:
Pattern.quote("$5");
It may be too late to respond, but you can also use Pattern.LITERAL
, which would ignore all special characters while formatting:
Pattern.compile(textToFormat, Pattern.LITERAL);
Difference between Pattern.quote
and Matcher.quoteReplacement
was not clear to me before I saw following example
s.replaceFirst(Pattern.quote("text to replace"),
Matcher.quoteReplacement("replacement text"));