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
🌐
Stack Overflow
stackoverflow.com › questions › 30219364 › replace-special-characters-with-character-references-in-string-using-java
Replace Special characters with Character references in string using java - Stack Overflow
May 13, 2015 - Finally, we can write a function ... string: public static String replaceSpecialChars(String s, ArrayList<StringTuple> specialChars){ for(StringTuple e: specialChars){ s = s.replaceAll(e.k, e.v); } return s; }...
🌐
DEV Community
dev.to › eyalk100 › the-complete-guide-to-java-string-replace-4jde
The Complete Guide to Java String Replace - DEV Community
March 1, 2021 - With replace(), you can replace an occurrence of a Character or String literal with another Character or String literal. You might use the String.replace() method in situations like: Replacing special characters in a String (eg, a String with ...
🌐
Stack Overflow
stackoverflow.com › questions › 20466112 › replace-special-character-from-string
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. ... Sign up to request clarification or add additional context in comments. ... public static void main(String[] args) { String str ="\'abc\'"; System.out.println(str.replace("\\","")); }this worked for me...
🌐
W3Schools
w3schools.com › java › ref_string_replace.asp
Java String replace() Method
Java Examples Java Videos Java ... Java Interview Q&A ... The replace() method searches a string for a specified character, and returns a new string where the specified character(s) are replaced....
🌐
Coderanch
coderanch.com › t › 406470 › java › Regex-replacing-special-characters
Regex for replacing special characters (Beginning Java forum at Coderanch)
March 19, 2007 - programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums · this forum made possible by our volunteer staff, including ... ... String name= "12E463~1.jpg"; String newName = name.replaceAll("[a-zA-Z1-90_\\- \\.]*","_"); String name has some value which contains some special characters.
Find elsewhere
🌐
Codementor
codementor.io › community › java regular expression: part 6 - replacing text
Java Regular Expression: part 6 - Replacing text | Codementor
April 5, 2019 - For instance, we need to replace error or redundant characters with the right ones. The java.lang.String class in Java supports 2 methods for these purposes: replace() and replaceAll() For instance, I have a string as follows:
🌐
Coderanch
coderanch.com › t › 454388 › java › replace-special-characters-string-java
replace special characters from string in the java (Servlets forum at Coderanch)
hi i had a relative path ie D:/Working/geotrak/web/reports/SpeedReport.jasper"; in this relative path i want to replase the Backslash(/) by Forwordslash(\) Can any one say me how to do this in java Example: String str="/Working/geotrak/web/reports/SpeedReport.jasper"; String newStr = str.replace ('//' , '\\' ); System.out.println("old = " + str); System.out.println("new = " + newStr); But it not Replashing this / from \; no change happen so Help me to correct it Thank inn Advance ... You've messed up the names. Your code doesn't even compile; forwardslash is a normal character, you just need to use one ('/').
🌐
Upgrad
upgrad.com › home › tutorials › software & tech › replace in java
Replace in Java | Replace Characters & Strings Explained
August 28, 2025 - This tutorial focuses on the Java string.replace() method, where a new one can replace every occurrence of the old character. This method returns a string to replace each old character with a new char or CharSequence.
Top answer
1 of 2
3

It's better in this case to use non-regex replace() instead of regex replaceAll(). You don't need regular expressions for this replacement and it complicates things because it needs extra escapes. Backslash is a special character in Java and also in regular expressions, so in Java if you want a straight backslash you have to double it up \\ and if you want a straight backslash in a regular expression in Java you have to quadruple it \\\\.

something = something.replace("\\", "/");

Behind the scenes, replace(String, String) uses regular expression patterns (at least in Oracle JDK) so has some overhead. In your specific case, you can actually use single character replacement, which may be more efficient (not that it probably matters!):

something = something.replace('\\', '/');

If you were to use regular expressions:

something = something.replaceAll("\\\\", "/");

Or:

something = something.replaceAll(Pattern.quote("\\"), "/");
2 of 2
0

To replace backslashes with replaceAll you'll have to escape them properly in the regular expression that you are using.

In your case the correct expression would be:

final String path = "C:\\Users\\SXR8036\\Downloads\\LANE-914.xls";
final String normalizedPath = path.replaceAll("\\\\", "/");

As the backslash itself is the escape character in Java Strings it needs to be escaped twice to work as desired.

In general you can pass very complex regular expressions to String.replaceAll. See the JavaDocs of java.lang.String.replaceAll and especially java.util.regex.Pattern for more information.