Java regular expression to remove all non alphanumeric characters EXCEPT spaces

前端 未结 5 1830
轮回少年
轮回少年 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:08

    You need to escape the \ so that the regular expression recognizes \s :

    paragraphInformation = paragraphInformation.replaceAll("[^a-zA-Z0-9\\s]", "");
    
    0 讨论(0)
  • Generally whenever you see that error, it means you only have a single backslash where you need two:

    paragraphInformation = paragraphInformation.replaceAll("[^a-zA-Z0-9\\s]", "");
    
    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2020-12-28 16:28

    Please take a look at this site, you can test Java Regex online and get wellformatted regex string patterns back:

    http://www.regexplanet.com/advanced/java/index.html

    0 讨论(0)
  • 2020-12-28 16:31

    Victoria, you must write \\s not \s here.

    0 讨论(0)
提交回复
热议问题