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!");
}
Please don't do that... little Unicode BABY ANGEL
s 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 AZ
"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:
[$&+,:;=?@#|'<>.^*()%!-]
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_]");
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: https://regexr.com/
Good luck;)
Here is my regex variant of a special character:
String regExp = "^[^<>{}\"/|;:.,~!?@#$%^=&*\\]\\\\()\\[¿§«»ω⊙¤°℃℉€¥£¢¡®©0-9_+]*$";
(Java code)
If 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.
Try:
(?i)^([[a-z][^a-z0-9\\s\\(\\)\\[\\]\\{\\}\\\\^\\$\\|\\?\\*\\+\\.\\<\\>\\-\\=\\!\\_]]*)$
(?i)^(A)$
: indicates that the regular expression A
is case insensitive.
[az]
: represents any alphabetic character from a
to z
.
[^a-z0-9\\\\s\\\\(\\\\)\\\\[\\\\]\\\\{\\\\}\\\\\\\\^\\\\$\\\\|\\\\?\\\\*\\\\+\\\\.\\\\<\\\\>\\\\-\\\\=\\\\!\\\\_]
: represents any alphabetic character except a
to z
, digits, and special characters ie accented characters.
[[az][^a-z0-9\\\\s\\\\(\\\\)\\\\[\\\\]\\\\{\\\\}\\\\\\\\^\\\\$\\\\|\\\\?\\\\*\\\\+\\\\.\\\\<\\\\>\\\\-\\\\=\\\\!\\\\_]]
: represents any alphabetic(accented or unaccented) character only characters.
*
: one or more occurrence of the regex that precedes it.
使用此正则表达式模式 ("^[a-zA-Z0-9]*$") 。它验证不包括特殊字符的字母数字字符串
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.
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 é, í ).
尝试将其用于相同的事情 - 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();
这是我的正则表达式,用于从任何字符串中删除所有特殊字符:
String regex = ("[ \\\\s@ [\\\"]\\\\[\\\\]\\\\\\\0-9|^{#%'*/<()>}:`;,!& .?_$+-]+")
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("");
Shout out to Mohamed Yusuff 's solution!
We can match all 32 special characters using range.
[!-\/:-@[-`{-~]
-- 15 out of 32 characters matched
-- 7 out of 32 characters matched
-- 6 out of 32 characters matched
-- 4 out of 32 characters matched
In total matched back all 32 chars (15+7+6+4)
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)
(^\\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}]+"
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.
我使用下面的 reg 在字符串中查找特殊字符
var reg = new RegExp("[`~!@#$%^&*()\\]\\[+={}/|:;\"\'<>,.?-_]");
A small addition to include all special characters like: ū
and Ā
:
An example:
Pattern regex = Pattern.compile("[A-Za-zÀ-ÖØ-öø-ū]");
You have to escape some symbols
/([!`\-\_.\"\'#%,:;<>=@{}~\$\(\)\*\+\/\\\?\[\]\^\|]+)/
OR
/([\!\"\#\$\%\&\'\(\)\*\+\,\-\.\/\:\;\<\>\=\?\@\[\]\{\}\\\\\^\_\`\~]+$)/
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.