Java regular expression to remove all non alphanumeric characters EXCEPT spaces

前端 未结 5 1831
轮回少年
轮回少年 2020-12-28 15:27

I\'m trying to write a regular expression in Java which removes all non-alphanumeric characters from a paragraph, except the spaces between the words.

This is the co

5条回答
  •  無奈伤痛
    2020-12-28 16:14

    You need to double-escape the \ character: "[^a-zA-Z0-9\\s]"

    Java will interpret \s as a Java String escape character, which is indeed an invalid Java escape. By writing \\, you escape the \ character, essentially sending a single \ character to the regex. This \ then becomes part of the regex escape character \s.

提交回复
热议问题