Is there a way to make strings safe for regex. What I mean is:
We have an application that uses regex to do some pattern finding. However, we don't want anything the user enters to interfere with this pattern finding.
So, what's happening is that some of the characters used by the user are creating regex searches (and throwing errors).
Is there a way to convert all regex special characters to safe versions? Or convert a subset?
Specifically, how do you convert a $ symbol to a safe version?
If you're running Java 5, you can either escape the string with the static Pattern.quote(String) method, or compile it with the Pattern.LITERAL flag set. For earlier releases, you can just escape all punctuation characters: