Java 正则表达式模式,包括所有特殊字符
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18057962/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Regex pattern including all special characters
提问by Piotr Sagalara
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!");
}
采纳答案by xanatos
Please don't do that... little Unicode BABY ANGELs like this one are dying! ??? (← these are not images) (nor is the arrow!)
请不要那样做……像这样的小 Unicode BABY ANGEL 快死了!???(← 这些不是图像)(箭头也不是!)
?
?
而你正在杀死 20 年的 DOS :-)(最后一个笑脸被称为 WHITE SMILING FACE白色的笑脸......现在是263A......但在古代它是ALT-1)and his friend
和他的朋友
?
?
BLACK SMILING FACE黑色笑脸……现在是263B……但在古代是ALT-2Try a negative match:
尝试否定匹配:
Pattern regex = Pattern.compile("[^A-Za-z0-9]");
(this will ok only A-Z "standard" letters and "standard" digits.
(这仅适用于 AZ“标准”字母和“标准”数字。
回答by Jerry
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:
在字符类中间有一个破折号,表示字符范围。将破折号放在课程末尾,如下所示:
[$&+,:;=?@#|'<>.^*()%!-]
回答by anubhava
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_]
这意味着匹配除 [A-Za-z0-9\s_]
Unicode version:
Unicode 版本:
Pattern regex = Pattern.compile("[^\p{L}\d\s_]");
回答by Sina Iravanian
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 unicodepunctuation and symbols.
其中包含所有 unicode标点符号和符号。
回答by Serguei Fedorov
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
如果您只依赖 ASCII 字符,则可以依赖使用 ASCII 表上的十六进制范围。这是一个正则表达式,它将获取33-47
, 58-64
, 91-96
,范围内的所有特殊字符123-126
[\x21-\x2F\x3A-\x40\x5B-\x60\x7B-\x7E]
However you can think of special characters as notnormal characters. If we take that approach, you can simply do this
但是,您可以将特殊字符视为非正常字符。如果我们采用这种方法,您可以简单地执行此操作
^[A-Za-z0-9\s]+
Hower this will not catch _
^
and probably others.
但是,这不会赶上_
^
,可能还有其他人。
回答by cdaiga
Try:
尝试:
(?i)^([[a-z][^a-z0-9\s\(\)\[\]\{\}\\^\$\|\?\*\+\.\<\>\-\=\!\_]]*)$
(?i)^(A)$
: indicates that the regular expression A
is case insensitive.
(?i)^(A)$
: 表示正则表达式A
不区分大小写。
[a-z]
: represents any alphabetic character from a
to z
.
[a-z]
: 代表从a
到的任何字母字符z
。
[^a-z0-9\\s\\(\\)\\[\\]\\{\\}\\\\^\\$\\|\\?\\*\\+\\.\\<\\>\\-\\=\\!\\_]
: represents any alphabetic character except a
to z
, digits, and special characters i.e. accented characters.
[^a-z0-9\\s\\(\\)\\[\\]\\{\\}\\\\^\\$\\|\\?\\*\\+\\.\\<\\>\\-\\=\\!\\_]
: 表示除a
to z
、数字和特殊字符(即重音字符)之外的任何字母字符。
[[a-z][^a-z0-9\\s\\(\\)\\[\\]\\{\\}\\\\^\\$\\|\\?\\*\\+\\.\\<\\>\\-\\=\\!\\_]]
: represents any alphabetic(accented or unaccented) character only characters.
[[a-z][^a-z0-9\\s\\(\\)\\[\\]\\{\\}\\\\^\\$\\|\\?\\*\\+\\.\\<\\>\\-\\=\\!\\_]]
: 代表任何字母(重音或非重音)字符。
*
: one or more occurrence of the regex that precedes it.
*
: 一个或多个出现在它之前的正则表达式。
回答by sam
Use this regular expression pattern ("^[a-zA-Z0-9]*$") .It validates alphanumeric string excluding the special characters
使用此正则表达式模式 ("^[a-zA-Z0-9]*$") 。它验证不包括特殊字符的字母数字字符串
回答by Sareesh Krishnan
(^\W$)
(^\W$)
^ - start of the string, \W - match any non-word character [^a-zA-Z0-9_], $ - end of the string
^ - 字符串的开头,\W - 匹配任何非单词字符 [^a-zA-Z0-9_],$ - 字符串的结尾
回答by Ash
Try using this for the same things - StringUtils.isAlphanumeric(value)
尝试将其用于相同的事情 - StringUtils.isAlphanumeric(value)
回答by Chuck
Here is my regex variant of a special character:
这是我的特殊字符的正则表达式变体:
String regExp = "^[^<>{}\"/|;:.,~!?@#$%^=&*\]\\()\[?§??ω⊙¤°℃℉¥£¢???0-9_+]*$";
(Java code)
(Java代码)