Java Regex: Remove Symbols from Strings
You can use character classes in regular expressions to match and remove any symbols. Character classes are represented by square brackets [], within which you can include the characters you want to match.
For example, to remove any symbols from a string, you can use the following regular expression:
String str = "Hello, World!@#$%^&*()_+-={}[]|\\:;\"'<>,.?/~`";
str = str.replaceAll("[^a-zA-Z0-9\\s]", "");
System.out.println(str);
The output is as follows:
Hello World
In the code above, str.replaceAll(“[^a-zA-Z0-9\\s]”, “”) will match all characters except for letters, numbers, and whitespace, and replace them with an empty string. This effectively removes any symbols.