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.
1 2 3 |
System.out.println( "abcd123".replaceAll("[@!#$]", "") ); System.out.println( "A@1".replaceAll("[@!#$]", "") ); System.out.println( "=abc@xyz12$$!#$adf".replaceAll("[@!#$]", "") ); |
Output
1 2 3 |
abcd123 A1 =abcxyz12adf |
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.
1 |
[^a-zA-Z0-9] |
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
package com.javacodeexamples.regex; public class RegExRemoveSpecialCharacters { public static void main(String[] args) { String[] strContent = { "abcABC", "123", "AB@CD$", "a~!@#$%^&*(){}+=\\|?/<>,.:;'", }; String pattern = "[^a-zA-Z0-9]"; for(String str : strContent) { System.out.println( str.replaceAll(pattern, "") ); } } } |
Output
1 2 3 4 |
abcABC 123 ABCD a |
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.
1 |
[^a-zA-Z0-9_] |
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.