java 正则表达式以允许特定的特殊字符
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/31462832/
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
Regular Expression to allow specific special characters
提问by gusadolfo
I'm currently validating a User Name input and I have to validate that certain special characters are allowed to be inserted into the input.
我目前正在验证用户名输入,我必须验证是否允许将某些特殊字符插入到输入中。
The White List of characters are:
字符的白名单是:
$#+{}:?.,~@"
and white spaces
$#+{}:?.,~@"
和空格
And the Black Listed characters are:
列入黑名单的字符是:
^$;\-()<>|='%_
^$;\-()<>|='%_
The validation should allow any alphanumeric character with one or more special characters and white spaces. It could also work with the black list case either way is fine.
验证应允许任何带有一个或多个特殊字符和空格的字母数字字符。无论哪种方式都可以,它也可以与黑名单案例一起使用。
I had this:
我有这个:
public static boolean alphNum(String value) {
Pattern p = Pattern.compile("^[\w ]*[^\W_][\w ]*$");
Matcher m = p.matcher(value);
return m.matches();
}
for only alphanumeric characters and white spaces but now the want that specific list of special characters to be allowed also.
仅适用于字母数字字符和空格,但现在还希望允许特定的特殊字符列表。
Here are some examples of the types I need to allow:
以下是我需要允许的类型的一些示例:
- Name1$
- Name1# Name2
- Name1 Name2
- Name Name2
- Name1 Name...
- 姓名1$
- 姓名1#姓名2
- 姓名 1 姓名 2
- 姓名姓名2
- 姓名 1 姓名...
I've seen a lot of regex validations but none this specific, if anyone could help me out with this I'd really appreciate it.
我见过很多正则表达式验证,但没有一个是具体的,如果有人能帮我解决这个问题,我会非常感激。
采纳答案by hair raisin
If there are no restrictions such as "must start with a letter" or "must contain at least one letter" (your question doesn't specify any), and assuming that by "white spaces" you mean a space, but not tabs, newlines, etc., then the expression would just be:
如果没有诸如“必须以字母开头”或“必须至少包含一个字母”(您的问题没有指定任何字母)等限制,并且假设“空格”是指空格,而不是制表符,换行符等,那么表达式就是:
Pattern.compile("^[\$#\+{}:\?\.,~@\"a-zA-Z0-9 ]+$");
Note that this regular expression allows things such as only a single space, or a user name identical to another one except for the number of trailing spaces, etc. if you want to be a little more restrictive and enforce starting with a letter or number (for example), you could do:
请注意,此正则表达式允许诸如仅一个空格或除尾随空格数之外的用户名与另一个相同的用户名等内容,如果您想更严格一些并强制以字母或数字开头(例如),你可以这样做:
Pattern.compile("^[a-zA-Z0-9][\$#\+{}:\?\.,~@\"a-zA-Z0-9 ]+$");
回答by Shar1er80
Use this pattern which allows characters that are not on your blacked listed characters.
使用此模式允许字符不在您的黑名单字符上。
"[^^;\-()<>|='%_]+"
The first ^
means NOTany of the following characters. Until you clarify if the $
is good or bad, I'm treating it as a good character.
第一个^
表示不是以下任何字符。在你澄清它$
是好是坏之前,我把它当作一个好角色。
Code sample:
代码示例:
public static void main(String[] args) throws Exception {
List<String> userNames = new ArrayList() {{
add("Name1$"); // Good
add("Name1# Name2"); // Good
add("Name1 Name2"); // Good
add("Name Name2"); // Good
add("Name1 Name"); // Good
add("UserName$"); // Good
add("UserName^"); // Bad
add("UserName;"); // Bad
add("User-Name"); // Bad
add("User_Name"); // Bad
}};
Pattern pattern = Pattern.compile("[^^;\-()<>|='%_]+");
for (String userName : userNames) {
if (pattern.matcher(userName).matches()) {
System.out.println("Good Username");
} else {
System.out.println("Bad Username");
}
}
}
Results:
结果:
Good Username
Good Username
Good Username
Good Username
Good Username
Good Username
Bad Username
Bad Username
Bad Username
Bad Username