Escape regular expression

If you have to insert any character array as literal you need to escape the reserved characters. For some of the typical formats the Java runtime library or the servlet api contains the necessary escape functions. For regular expressions used in java.util.regex.Pattern such a method is missing:

public static final SPECIAL_CHARS = "\\.$^()[]{}+*";
public static String escape(String value) {
StringBuilder expr = new StringBuilder();
for (char c : value.toCharArray()) {
if (SPECIAL_CHARS.indexOf(c) != -1) {
return expr.toString();