Remove special characters in the string in java?
问题 How to remove special characters in the string except "- _". Now I use: replaceAll("[^\\w\\s]", "") it remove all special character but i want to keep "- _" . Can anyone tell me how should I do? 回答1: Use replaceAll("[^\\w\\s\\-_]", ""); What I did was add the underscore and hyphen to the regular expression. I added a \\ before the hyphen because it also serves for specifying ranges: a-z means all letters between a and z. Escaping it with \\ makes sure it is treated as an hyphen. 回答2: This