Regex pattern including all special characters

后端 未结 18 1618
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-12-02 09:18

I want to write a simple regular expression to check if in given string exist any special character. My regex works but I don\'t know why it also includes all numbers, so wh

18条回答
  •  孤街浪徒
    2020-12-02 09:45

    Please use this.. it is simplest.

    \p{Punct} Punctuation: One of !"#$%&'()*+,-./:;<=>?@[]^_`{|}~

    https://docs.oracle.com/javase/7/docs/api/java/util/regex/Pattern.html

        StringBuilder builder = new StringBuilder(checkstring);
        String regex = "\\p{Punct}"; //Special character : `~!@#$%^&*()-_+=\|}{]["';:/?.,><
        //change your all special characters to "" 
        Pattern  pattern = Pattern.compile(regex);
        Matcher matcher = pattern.matcher(builder.toString());
        checkstring=matcher.replaceAll("");
    

提交回复
热议问题