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
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)
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}]+"
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:
[$&+,:;=?@#|'<>.^*()%!-]
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.
Here is my regular expression, that I used for removing all the special characters from any string :
String regex = ("[ \\\\s@ [\\\"]\\\\[\\\\]\\\\\\\0-9|^{#%'*/<()>}:`;,!& .?_$+-]+")
Please don't do that... little Unicode BABY ANGELs like this one