Skip to content

Java RegEx – Remove All Special Characters from String

Java RegEx – Remove All Special Characters from String example shows how to remove all special characters from a string using regex pattern in Java.

How to remove special characters from a string using a regex pattern?

There are many cases where we do not want to have any special characters inside string content. For example, in user-generated content or in the string used for id.

If there is a fixed list of characters you do not want in the string, you can simply list all of them in a character class and remove all of them using the string replaceAll method. For example, if you do not want any of the “@!#$” characters, you can use below given regex pattern.

Output

As you can see from the output, the regex pattern removed all the characters we specified in the character class from the string data. However, the “=” was not removed from the last string since it was not on our list of characters.

Use the above-given approach if you have a limited number of blacklist characters that you do not want to keep in the string. If you want to remove all the special characters, you can simply use the below-given pattern.

The pattern means not any character between a to z, A-Z, and 0 to 9. That basically means it will remove everything except the characters we specified when we use it to replace the match with an empty string.

Output

As you can see from the output, all the special characters were removed from the string this time.

Important Note:

The above-given pattern removes everything that is not a character or a digit. If you want to allow a few or some of the characters e.g. an underscore you can mention that in the character class. See the below-given pattern.

If you want to learn more about regex, please visit the Java Regex Tutorial.

Please let me know your views in the comments section below.

About the author

Leave a Reply

Your email address will not be published.