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 when I put some number it returns an error.
My code:
//pattern to find if there is any special character in string
Pattern regex = Pattern.compile("[$&+,:;=?@#|'<>.-^*()%!]");
//matcher to find if there is any special character in string
Matcher matcher = regex.matcher(searchQuery.getSearchFor());
if(matcher.find())
{ errors.rejectValue("searchFor", "wrong_pattern.SearchQuery.searchForSpecialCharacters","Special characters are not allowed!");
} 6 19 Answers
Please don't do that... little Unicode BABY ANGELs like this one 👼 are dying! ◕◡◕ (← these are not images) (nor is the arrow!)
☺
And you are killing 20 years of DOS :-) (the last smiley is called WHITE SMILING FACE... Now it's at 263A... But in ancient times it was ALT-1)
and his friend
☻
BLACK SMILING FACE... Now it's at 263B... But in ancient times it was ALT-2
Try a negative match:
Pattern regex = Pattern.compile("[^A-Za-z0-9]");(this will ok only A-Z "standard" letters and "standard" 0-9 digits.)
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:
[$&+,:;=?@#|'<>.^*()%!-] 2 That's because your pattern contains a .-^ which is all characters between and including . and ^, which included digits and several other characters as shown below:
If by special characters, you mean punctuation and symbols use:
[\p{P}\p{S}]which contains all unicode punctuation and symbols.
SInce you don't have white-space and underscore in your character class I think following regex will be better for you:
Pattern regex = Pattern.compile("[^\w\s]");Which means match everything other than [A-Za-z0-9\s_]
Unicode version:
Pattern regex = Pattern.compile("[^\p{L}\d\s_]"); 0 For people (like me) looking for an answer for special characters like Ä etc. just use this pattern:
Only text (or a space): "[A-Za-zÀ-ȕ ]"
Text and numbers: "[A-Za-zÀ-ȕ0-9 ]"
Text, numbers and some special chars: "[A-Za-zÀ-ȕ0-9(),-_., ]"
Regex just starts at the ascii index and checks if a character of the string is in within both indexes [startindex-endindex].
So you can add any range.
Eventually you can play around with a handy tool:
Good luck;)
Use this to catch the common special characters excluding .-_.
/[!"`'#%&,:;<>=@{}~\$\(\)\*\+\/\\\?\[\]\^\|]+/If you want to include .-_ as well, then use this:
/[-._!"`'#%&,:;<>=@{}~\$\(\)\*\+\/\\\?\[\]\^\|]+/If you want to filter strings that are URL friendly and do not contain any special characters or spaces, then use this:
/^[^ !"`'#%&,:;<>=@{}~\$\(\)\*\+\/\\\?\[\]\^\|]+$/When you use patterns like /[^A-Za-z0-9]/, then you will start catching special alphabets like those of other languages and some European accented alphabets (like é, í ).
Here is my regex variant of a special character:
String regExp = "^[^<>{}\"/|;:.,~!?@#$%^=&*\\]\\\\()\\[¿§«»ω⊙¤°℃℉€¥£¢¡®©0-9_+]*$";(Java code)
0If you only rely on ASCII characters, you can rely on using the hex ranges on the ASCII table. Here is a regex that will grab all special characters in the range of 33-47, 58-64, 91-96, 123-126
[\x21-\x2F\x3A-\x40\x5B-\x60\x7B-\x7E]However you can think of special characters as not normal characters. If we take that approach, you can simply do this
^[A-Za-z0-9\s]+Hower this will not catch _ ^ and probably others.
I have defined one pattern to look for any of the ASCII Special Characters ranging between 032 to 126 except the alpha-numeric. You may use something like the one below:
To find any Special Character:
[ -\/:-@\[-\`{-~]To find minimum of 1 and maximum of any count:
(?=.*[ -\/:-@\[-\`{-~]{1,})
These patterns have Special Characters ranging between 032 to 047, 058 to 064, 091 to 096, and 123 to 126.
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.
Use this regular expression pattern ("^[a-zA-Z0-9]*$") .It validates alphanumeric string excluding the special characters
Try using this for the same things - StringUtils.isAlphanumeric(value)
We can achieve this using Pattern and Matcher as follows:
Pattern pattern = Pattern.compile("[^A-Za-z0-9 ]");
Matcher matcher = pattern.matcher(trString);
boolean hasSpecialChars = matcher.find(); Here is my regular expression, that I used for removing all the special characters from any string :
String regex = ("[ \\\\s@ [\\\"]\\\\[\\\\]\\\\\\\0-9|^{#%'*/<()>}:`;,!& .?_$+-]+") 1 Please use this.. it is simplest.
\p{Punct} Punctuation: One of !"#$%&'()*+,-./:;<=>?@[]^_`{|}~
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(""); 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)
To find any number of special characters use the following regex pattern:([^(A-Za-z0-9 )]{1,})
[^(A-Za-z0-9 )] this means any character except the alphabets, numbers, and space. {1,0} this means one or more characters of the previous block.
1(^\W$)
^ - start of the string, \W - match any non-word character [^a-zA-Z0-9_], $ - end of the string
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}]+"