I need to remove some special “special” characters and replace them with an empty string if they appear. I am currently having a problem with regex, possibly with Java escaping. I can’t bring them all together, it just doesn’t work, I tried a lot! T_T
I am currently doing this one by one, which is a bit silly, but at the moment, at least it works, for example:
public static String filterSpecialCharacters(String string) { string = string.replaceAll("-", ""); string = string.replaceAll("\\[", ""); string = string.replaceAll("\\]", ""); string = string.replaceAll("\\^", ""); string = string.replaceAll("/", ""); string = string.replaceAll(",", ""); string = string.replaceAll("'", ""); string = string.replaceAll("\\*", ""); string = string.replaceAll(":", ""); string = string.replaceAll("\\.", ""); string = string.replaceAll("!", ""); string = string.replaceAll(">", ""); string = string.replaceAll("<", ""); string = string.replaceAll("~", ""); string = string.replaceAll("@", ""); string = string.replaceAll("#", ""); string = string.replaceAll("$", ""); string = string.replaceAll("%", ""); string = string.replaceAll("\\+", ""); string = string.replaceAll("=", ""); string = string.replaceAll("\\?", ""); string = string.replaceAll("|", ""); string = string.replaceAll("\"", ""); string = string.replaceAll("\\\\", ""); string = string.replaceAll("\\)", ""); string = string.replaceAll("\\(", ""); return string; }
These are all the characters I need to remove:
- [ ] ^ / , ' * : . ! > < ~ @ # $ % + = ? | " \ ) (
There is clearly something missing for me, I can’t understand how to do all this on one line. Help?