How to replace all non-alphabetic characters and keep the spaces?

daria :

I am trying to replace all characters that are not letters in a string, but keep the spaces between words.

The code I have so far:

String newWords = words.replaceAll("[^A-Za-z0-9 ]", "");

I also tried this one:

[^a-zA-Z0-9\\s]

The problem with the code is that it removes everything but the numbers in the string.

Pshemo :

It deletes the dot between the 87.97, but wont remove the numbers itself

[^...] means every character except. So [^A-Za-z0-9 ] means: match (and replace/remove) everything except A-Z a-z 0-9 and space. In other words those characters are protected from removal.

If you also want to remove numbers (series of digits) remove 0-9 from your regex.

String newWords = words.replaceAll("[^A-Za-z ]", "");

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=349066&siteId=1