That depends on what you mean. If you just want to get rid of them, do this:
(Update: Apparently you want to keep digits as well, use the second lines in that case)

String alphaOnly = input.replaceAll("[^a-zA-Z]+","");
String alphaAndDigits = input.replaceAll("[^a-zA-Z0-9]+","");

or the equivalent:

String alphaOnly = input.replaceAll("[^\\p{Alpha}]+","");
String alphaAndDigits = input.replaceAll("[^\\p{Alpha}\\p{Digit}]+","");

(All of these can be significantly improved by precompiling the regex pattern and storing it in a constant)

Or, with Guava:

private static final CharMatcher ALNUM =
  CharMatcher.inRange('a', 'z').or(CharMatcher.inRange('A', 'Z'))
  .or(CharMatcher.inRange('0', '9')).precomputed();
// ...
String alphaAndDigits = ALNUM.retainFrom(input);

But if you want to turn accented characters into something sensible that's still ascii, look at these questions:

  • Converting Java String to ASCII
  • Java change áéőűú to aeouu
  • ń ǹ ň ñ ṅ ņ ṇ ṋ ṉ ̈ ɲ ƞ ᶇ ɳ ȵ --> n or Remove diacritical marks from unicode chars
Answer from Sean Patrick Floyd on Stack Overflow
🌐
Coderanch
coderanch.com › t › 406470 › java › Regex-replacing-special-characters
Regex for replacing special characters (Beginning Java forum at Coderanch)
March 19, 2007 - Originally posted by srikanth ramu: This will replace all the special characters except dot (.) String newName = name.replaceAll("[\\W]&&[^.]","_"); Thanks for replying. But I want all the characters apart from A-Z a-z 0-9 . (dot) _ (underscore) - (hyphen) to be replaced with an _ (underscore) ... To invert a character class, add '^' to the beginning of it: Also, you want to use the + (one or more) quantifier instead of * (zero or more).
🌐
Know Program
knowprogram.com › home › replace special characters in java
Replace Special Characters In Java - Know Program
December 5, 2022 - Parameter:- regex – the regular expression to which this string is to be matched; replacement – the string to be substituted for each match. Return:- The resulting String. Throws:- PatternSyntaxException – if the regular expression’s syntax is invalid. Java replace special characters by using replaceAll()
🌐
Codementor
codementor.io › community › java regular expression: part 6 - replacing text
Java Regular Expression: part 6 - Replacing text | Codementor
April 5, 2019 - String s2 = "This is te$xt wi%th s\*ome spe!cial characters in in"; String nonePrintableCharsPattern = "[^\\w ]"; String newString2 = s2.replaceAll(nonePrintableCharsPattern, ""); System.out.println(newString2); I have another text containing some unintended special characters: the dollar sign, the percentage sign, the star character, and the exclamation mark.
Find elsewhere
🌐
Stack Overflow
stackoverflow.com › questions › 38722851 › replace-special-characters-in-java
text - replace special characters in java - Stack Overflow
I have an Arabic string, that I need to remove all special characters, LATIN ALPHABET , punctuation e.g. (, . ;) ,and Arabic punctuation e.g. (َ ً ُ ِ) I have wrote the following code · String input = "some text"; Pattern p = Pattern.compile("[\\p{P}\\w]"); java.util.regex.Matcher m = p.matcher(input); while (m.find()) { } m.reset(); input = m.replaceAll(" "); p = Pattern.compile("[\\p{Mn}\\p{Nd}\\p{InLatin-1Supplement}]+"); m = p.matcher(input); while (m.find()) { } m.reset(); input = m.replaceAll("");
🌐
Baeldung
baeldung.com › home › java › core java › guide to escaping characters in java regexps
Guide to Escaping Characters in Java RegExps | Baeldung
July 22, 2024 - We should note that Pattern.quote encloses the whole block with a single escape sequence. If we wanted to escape characters individually, we would need to use a token replacement algorithm. Let’s try to escape all special characters in a given string.
🌐
Blogger
javarevisited.blogspot.com › 2016 › 02 › how-to-remove-all-special-characters-of-String-in-java.html
How to remove all special characters from String in Java? Example Tutorial
Similarly, if you String contains many special characters, you can remove all of them by just picking alphanumeric characters e.g. replaceAll("[^a-zA-Z0-9_-]", ""), which will replace anything with empty String except a to z, A to Z, 0 to 9,_ ...
🌐
Stack Overflow
stackoverflow.com › questions › 20487897
java - Replace special character from String - Stack Overflow
For e.g. char myChar = 'a', when this expression comes to me(java level) we get myChar = \'a\'. So we need to print the same as "[myChar = 'a']". Now, its printing with special character. ... public static void main(String[] args) { String str ="\'abc\'"; System.out.println(str.replace("\\","")); }this worked for me... Actually I got confuzed with what will be my input, so the input will be \'abc\' and I can use replace("\\","") and that will solve my problem.
🌐
BeginnersBook
beginnersbook.com › 2024 › 06 › remove-special-characters-from-a-string-in-java
Remove special characters from a String in Java
June 1, 2024 - #Good -morning!"; //calling the ... matches any character that is not an // uppercase letter, lowercase letter, digit, or whitespace. String regex = "[^a-zA-Z0-9\\s]"; // Replace all special characters with an empty string // Note: There is no space between double quotes ...