Escaping a regex expression

I was working on a project where I needed to dynamically generate a regular expression based on some user input but the string to search for needed to be escaped. Below is the quick and dirty way to do it:
 * Utility function to replace regular expressions.
 * @param input
 * @return
private static String escapeRegex(String input)
    String special[] = { "\\", "[", "]", "^", "$", ".", "|", "?", "*", "+", "(", ")" };
    String val = input;
    for (String replace : special)
        val = val.replace(replace, "\\" + replace);

    return val;


  1. Slot Factory slots offer the most immersive mobile gaming with popular games like Money Grows on Tree, Fishin' for Wins and Big Box Bonus. Push Gaming focus on to} quality mobile leisure - creating the most effective participant experiences. From traditional games to Jammin' Jars and Razor Shark, to the buzzing Wild Swarm - 우리카지노 you may find their most popular games proper here at MrQ.


Post a Comment