public String escape(String valueToEscape) {
StringBuilder sb = new StringBuilder();
String specialCharactersOfRegex = ".\\+*?[^]$(){}=!<>|:-";
for (int i = 0; i < valueToEscape.length(); i++) {
// get character one by one
char currentChar = valueToEscape.charAt(i);
// does current character include in special characters list ?
if (specialCharactersOfRegex.indexOf(currentChar) != -1) {
sb.append("\\" + currentChar);
}
else {
sb.append(currentChar);
}
}
return sb.toString();
}