Convert java string to string compatible with a regex in replaceAll

Kevin Colin picture Kevin Colin · Jun 21, 2013 · Viewed 13.5k times · Source

Is there a library or any easy way to convert a string and make sure its compatible as a regex to look for and replace in another string. So if the string is "$money" it would get converted to "\$money". I tried using StringEscapeUtil.escape but it doesn't work with characters such as $.

Answer

Reimeus picture Reimeus · Jun 21, 2013