There's probably a more concise regex, but this will certainly work:

string.replaceAll("[^a-zA-Z0-9]", "");
Answer from stevevls on Stack Overflow
🌐
Alvin Alexander
alvinalexander.com › java › java-strip-characters-string-letters-numbers-replace
Java: How to strip unwanted characters from a string | alvinalexander.com
As you might guess, you can strip all characters but letters and numbers by making a minor change to the replaceAll regular expression, like this: ... All I did there was add the numbers [0-9] to our previous range of characters. As you can see from these examples, the Java String class replaceAll ...
🌐
GeeksforGeeks
geeksforgeeks.org › java › how-to-remove-all-non-alphanumeric-characters-from-a-string-in-java
How to remove all non-alphanumeric characters from a string in Java - GeeksforGeeks
July 15, 2025 - // Java program to remove non-alphanumeric // characters from a string class GFG { // Main driver method public static void main(String args[]) { // Input string String str1 = "@!Geeks-for'Geeks, 123"; str1 = str1.replaceAll("[^a-zA-Z0-9]", ""); System.out.println(str1); } }
🌐
PREP INSTA
prepinsta.com › home › dsa in java › java program to remove characters in a string except alphabets
Remove character in a String except alphabet | PrepInsta
October 13, 2022 - import java.util.Scanner; class Main { public static void main(String[] args) { String s = "hel1456lo56wor%^ld"; s=s.replaceAll("[^a-zA-Z]",""); System.out.println(s); } }
🌐
imalittletester
imalittletester.com › 2017 › 09 › 12 › removing-all-digits-non-digits-whitespaces-from-strings-and-other-usages-of-replaceall
Removing all digits, non-digits, whitespaces (from Strings) and other usages of replaceAll | imalittletester
April 12, 2020 - Or remove all numeric characters. Or maybe remove all white spaces. For this, the replaceAll method comes in handy. The replaceAll method is part of Java’s String class, and is documented here: https://docs.oracle.com/javase/8/docs/api/java/lang/String.html#replaceAll-java.lang.String-java.lang.String-.
🌐
Quora
quora.com › How-can-I-remove-all-non-alphanumeric-characters-from-a-string-in-Java
How to remove all non-alphanumeric characters from a string in Java - Quora
Answer: Here you go! Non-alphanumeric characters comprise of all the characters except alphabets and numbers. It can be punctuation characters like exclamation mark(!), at symbol(@), commas(, ), question mark(?), colon(:), dash(-) etc and special characters like dollar sign($), equal symbol(=), ...
Find elsewhere
🌐
CodingTechRoom
codingtechroom.com › question › remove-except-characters-string-java
How to Remove All Except Certain Characters from a String in Java? - CodingTechRoom
Understanding Java String manipulation. Familiarity with regex syntax. Recognizing the need to exclude certain characters and include others. Use regex for concise code: `String filtered = original.replaceAll("[^a-zA-Z ]", "");` to keep only letters and spaces.
🌐
Baeldung
baeldung.com › home › java › java string › retain only digits and decimal separator in string
Retain Only Digits and Decimal Separator in String | Baeldung
January 18, 2024 - To remove all non-numeric characters but keep the decimal separator in a Java String using Guava, we’ll use methods from the CharMatcher utility class.
🌐
Real's HowTo
rgagnon.com › javadetails › java-remove-non-numeric-character-in-a-string.html
Remove non numeric character in a String - Real's Java How-to
Got it Using a regular expression to filter all the non-numeric characters and replace them with an empty string.
🌐
CodingTechRoom
codingtechroom.com › question › java-remove-non-numeric-characters-string
How to Remove All Non-Numeric Characters from a String in Java Using Regular Expressions - CodingTechRoom
The regex pattern `[^0-9]` matches any character that is not a number, allowing you to remove them by replacing them with an empty string. Mistake: Using the wrong regex pattern, such as "[0-9]", which would only retain digits and remove everything else. Solution: Ensure to use the pattern ...
🌐
GeeksforGeeks
geeksforgeeks.org › java › remove-all-non-alphabetical-characters-of-a-string-in-java
Remove all non-alphabetical characters of a String in Java - GeeksforGeeks
March 24, 2023 - In this approach, we use the replaceAll() method to replace all non-alphabetic characters with an empty string. The regular expression "[^a-zA-Z]" matches any character that is not an English alphabetical letter (both uppercase and lowercase).