Remove special characters in the string in java?

前端 未结 7 2104
爱一瞬间的悲伤
爱一瞬间的悲伤 2021-01-15 08:28

How to remove special characters in the string except \"- _\". Now I use:

replaceAll(\"[^\\\\w\\\\s]\", \"\")

it remove all special charact

7条回答
  •  醉梦人生
    2021-01-15 09:04

    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.

提交回复
热议问题