That depends on what you define as special characters, but try replaceAll(...):

String result = yourString.replaceAll("[-+.^:,]","");

Note that the ^ character must not be the first one in the list, since you'd then either have to escape it or it would mean "any but these characters".

Another note: the - character needs to be the first or last one on the list, otherwise you'd have to escape it or it would define a range ( e.g. :-, would mean "all characters in the range : to ,).

So, in order to keep consistency and not depend on character positioning, you might want to escape all those characters that have a special meaning in regular expressions (the following list is not complete, so be aware of other characters like (, {, $ etc.):

String result = yourString.replaceAll("[\\-\\+\\.\\^:,]","");


If you want to get rid of all punctuation and symbols, try this regex: \p{P}\p{S} (keep in mind that in Java strings you'd have to escape back slashes: "\\p{P}\\p{S}").

A third way could be something like this, if you can exactly define what should be left in your string:

String  result = yourString.replaceAll("[^\\w\\s]","");

This means: replace everything that is not a word character (a-z in any case, 0-9 or _) or whitespace.

Edit: please note that there are a couple of other patterns that might prove helpful. However, I can't explain them all, so have a look at the reference section of regular-expressions.info.

Here's less restrictive alternative to the "define allowed characters" approach, as suggested by Ray:

String  result = yourString.replaceAll("[^\\p{L}\\p{Z}]","");

The regex matches everything that is not a letter in any language and not a separator (whitespace, linebreak etc.). Note that you can't use [\P{L}\P{Z}] (upper case P means not having that property), since that would mean "everything that is not a letter or not whitespace", which almost matches everything, since letters are not whitespace and vice versa.

Additional information on Unicode

Some unicode characters seem to cause problems due to different possible ways to encode them (as a single code point or a combination of code points). Please refer to regular-expressions.info for more information.

Answer from Thomas on Stack Overflow
🌐
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 ...
🌐
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
Let's see a couple fo examples to remove all special characters from String in Java. By the way, if you are a complete beginner into Regular expression and don't understand these magical symbols then I highly recommend you join The Complete Regular Expression course for beginners course on Udemy. It's a great course to learn everything about RegEx ...
Discussions

Remove all the special characters using java.util.regex - Oracle Forums
Hi, How to remove the all the special characters in a String[] using regex, i have the following:- public class RegExpTest { private static String removeSplCharactersForNumber(String[] number) { ... More on forums.oracle.com
🌐 forums.oracle.com
December 22, 2009
regex - remove all special characters in java - Stack Overflow
@var___ note that i used replaceAll ... take regex, thus it works .. :) 2013-01-16T16:52:20.733Z+00:00 ... This also replaces all white spaces. So, will not be a good idea to apply it on a sentence. 2017-01-16T12:18:05.917Z+00:00 ... You can read the lines and replace all special characters safely this ... More on stackoverflow.com
🌐 stackoverflow.com
regex - Remove special characters in the string in java? - Stack Overflow
How to remove special characters in the string except "- _". More on stackoverflow.com
🌐 stackoverflow.com
regex - Remove white space and special characters in Java - Stack Overflow
I have a String which contains some special characters and white spaces as well. I want to remove white spaces and special character. I am doing it as: String str = "45,%$^ Sharma%$&^,is,4... More on stackoverflow.com
🌐 stackoverflow.com
Top answer
1 of 9
294

That depends on what you define as special characters, but try replaceAll(...):

String result = yourString.replaceAll("[-+.^:,]","");

Note that the ^ character must not be the first one in the list, since you'd then either have to escape it or it would mean "any but these characters".

Another note: the - character needs to be the first or last one on the list, otherwise you'd have to escape it or it would define a range ( e.g. :-, would mean "all characters in the range : to ,).

So, in order to keep consistency and not depend on character positioning, you might want to escape all those characters that have a special meaning in regular expressions (the following list is not complete, so be aware of other characters like (, {, $ etc.):

String result = yourString.replaceAll("[\\-\\+\\.\\^:,]","");


If you want to get rid of all punctuation and symbols, try this regex: \p{P}\p{S} (keep in mind that in Java strings you'd have to escape back slashes: "\\p{P}\\p{S}").

A third way could be something like this, if you can exactly define what should be left in your string:

String  result = yourString.replaceAll("[^\\w\\s]","");

This means: replace everything that is not a word character (a-z in any case, 0-9 or _) or whitespace.

Edit: please note that there are a couple of other patterns that might prove helpful. However, I can't explain them all, so have a look at the reference section of regular-expressions.info.

Here's less restrictive alternative to the "define allowed characters" approach, as suggested by Ray:

String  result = yourString.replaceAll("[^\\p{L}\\p{Z}]","");

The regex matches everything that is not a letter in any language and not a separator (whitespace, linebreak etc.). Note that you can't use [\P{L}\P{Z}] (upper case P means not having that property), since that would mean "everything that is not a letter or not whitespace", which almost matches everything, since letters are not whitespace and vice versa.

Additional information on Unicode

Some unicode characters seem to cause problems due to different possible ways to encode them (as a single code point or a combination of code points). Please refer to regular-expressions.info for more information.

2 of 9
72

This will replace all the characters except alphanumeric

replaceAll("[^A-Za-z0-9]","");
🌐
Oracle
forums.oracle.com › ords › apexds › post › remove-all-the-special-characters-using-java-util-regex-3469
Remove all the special characters using java.util.regex - Oracle Forums
December 22, 2009 - Hi, How to remove the all the special characters in a String[] using regex, i have the following:- public class RegExpTest { private static String removeSplCharactersForNumber(String[] number) { ...
🌐
Coderanch
coderanch.com › t › 720854 › java › Remove-special-characters-regular-expression
Remove special characters/regular expression from string. (Java in General forum at Coderanch)
Knute Snortum wrote:...how about [^#]*#(.*) Gives a different output. I think OP wants the last occurrence of # followed by the word. My regex prints "ranch" for "I#love#code#ranch" and your regex prints "love#code#ranch"
🌐
Ebhor
ebhor.com › home › how to remove special characters from a string in java
How to remove special characters from a string in java - Ebhor.com
August 13, 2023 - To remove special characters we will use the String class method replaceAll(). This method takes two arguments. First is String regular expression (regex) that matched the substring that the user wants to replace.
Find elsewhere
🌐
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 - The answer is that we need to escape the dot (.) character so that its special meaning is ignored. Let’s dig into it in more detail in the next section. According to the Java API documentation for regular expressions, there are two ways in which we can escape characters that have special meaning.
🌐
Silicon Cloud
silicloud.com › home › how to remove special characters using java regular expressions?
How to remove special characters using Java regular expressions? - Blog - Silicon Cloud
March 21, 2024 - In the above code, a regular expression pattern is created using Pattern.compile(“[^a-zA-Z0-9]”), where [^a-zA-Z0-9] represents all characters except letters and numbers. Then, use the Matcher.replaceAll(“”) method to replace special characters with an empty string, obtaining the result ...
🌐
YouTube
youtube.com › watch
Remove Special Characters from a String using Regular Expression in Java #shorts #javainterview - YouTube
Remove Special Characters from a String using Regular Expression in Java #shorts #javainterview
Published   March 20, 2023
🌐
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).
🌐
Codementor
codementor.io › community › java regular expression: part 6 - replacing text
Java Regular Expression: part 6 - Replacing text | Codementor
April 5, 2019 - Since there are many special characters, it is easier to specify in the patterns what we want to keep, rather than what we want to remove. So in the pattern, I use the caret character (^) followed by \w and a whitespace.
🌐
Quora
quora.com › How-do-you-remove-special-and-space-characters-in-Java
How to remove special and space characters in Java - Quora
Answer: Java uses replaceAll() method. This method replaces each substring matching the given regular expression with the given replacement. A String strInput example shows this. Java uses String trim() method. This method removes leading and trailing whitespaces. This whitespace character unicod...
🌐
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 - The string can be easily filtered using the ReGex [^a-zA-Z0-9 ]. Java · // 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); } } Output ·
🌐
Javatpoint
javatpoint.com › how-to-remove-special-characters-from-string-in-java
How to Remove Special Characters from String in Java - Javatpoint
How to Remove Special Characters from String in Java with oops, string, exceptions, multithreading, collections, jdbc, rmi, fundamentals, programs, swing, javafx, io streams, networking, sockets, classes, objects etc,
🌐
Blogger
javarevisited.blogspot.com › 2016 › 02 › how-to-remove-all-special-characters-of-String-in-java.html
Javarevisited: How to remove all special characters from String in Java? Example Tutorial
Here is our sample Java program to demonstrate how you can use the replaceAll() method to remove all special characters from a String in Java.