String userInput = "Hello, World! ££$$%%##";
String onlyAlphaNumeric = userInput.replaceAll("[^a-zA-Z0-9]", "");
System.out.println(onlyAlphaNumeric);
Prints out
HelloWorld
Here are some useful Java regular expressions:
Only numbers
[^0-9]
Only alpha (upper and lower case letters)
[^a-zA-Z]
Only alphanumeric (upper and lower case letters, numbers)
[^a-zA-Z0-9]
Alphanumeric with space
[^a-zA-Z0-9 ]