Regex pattern including all special characters

后端 未结 18 1539
爱一瞬间的悲伤
爱一瞬间的悲伤 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:28

    You can use a negative match:

    Pattern regex = Pattern.compile("([a-zA-Z0-9])*"); (For zero or more characters)

    or

    Pattern regex = Pattern.compile("([a-zA-Z0-9])+"); (For one or more characters)

    0 讨论(0)
  • 2020-12-02 09:29

    Try this. It works on C# it should work on java also. If you want to exclude spaces just add \s in there @"[^\p{L}\p{Nd}]+"

    0 讨论(0)
  • 2020-12-02 09:30

    You have a dash in the middle of the character class, which will mean a character range. Put the dash at the end of the class like so:

    [$&+,:;=?@#|'<>.^*()%!-]
    
    0 讨论(0)
  • 2020-12-02 09:30

    Try:

    (?i)^([[a-z][^a-z0-9\\s\\(\\)\\[\\]\\{\\}\\\\^\\$\\|\\?\\*\\+\\.\\<\\>\\-\\=\\!\\_]]*)$
    

    (?i)^(A)$: indicates that the regular expression A is case insensitive.

    [a-z]: represents any alphabetic character from a to z.

    [^a-z0-9\\s\\(\\)\\[\\]\\{\\}\\\\^\\$\\|\\?\\*\\+\\.\\<\\>\\-\\=\\!\\_]: represents any alphabetic character except a to z, digits, and special characters i.e. accented characters.

    [[a-z][^a-z0-9\\s\\(\\)\\[\\]\\{\\}\\\\^\\$\\|\\?\\*\\+\\.\\<\\>\\-\\=\\!\\_]]: represents any alphabetic(accented or unaccented) character only characters.

    *: one or more occurrence of the regex that precedes it.

    0 讨论(0)
  • 2020-12-02 09:32

    Here is my regular expression, that I used for removing all the special characters from any string :

    String regex = ("[ \\\\s@  [\\\"]\\\\[\\\\]\\\\\\\0-9|^{#%'*/<()>}:`;,!& .?_$+-]+")
    
    0 讨论(0)
  • 2020-12-02 09:34

    Please don't do that... little Unicode BABY ANGELs like this one

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