Click here to Skip to main content
15,902,114 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
How do I replace all non-alphanumerical characters & spaces with empty spaces?

I have this but it only removes spaces, I want it to remove all NON-alphanumerical characters:

str.replaceAll("\\W","")



Thank you for the help in advance!


~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

What I have tried:

str.replaceAll("\\W","")


~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Posted
Updated 26-Sep-17 18:19pm

1 solution

Try the following regular expression

PERL
[^a-zA-Z0-9]+

The example follows
echo "abc---012" | sed -e 's/[^0-9a-zA-Z][^0-9a-zA-Z]*/ /g'
#output
abc 012


FYI: .+ is equivalent to ..*

PERL
[^a-zA-Z0-9]

example follows
echo "abc---012" | sed -e 's/[^0-9a-zA-Z]/ /g'
#output
abc   012
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900