如何在JAVA中使用regexp查找字符串中包含的字符,例如ľščťžýáí,不是来自美国字母表,(abcde ... XYZ)?我试了[^ a-z],p {L},没有成功。我想拒绝包含诸如变形金刚,'等等字符的密码...我不想手动列出所有字符因为它可能是巨大的数字所以我更愿意列出只允许的字符(aZ )。感谢
Passwörd9:无效
更新: 我需要检查密码是否遵循这些规则:
我使用这个正则表达式和正向前瞻,但它似乎与变音符号,变音符号等无法正常工作。
private static final Pattern PATTERN = Pattern.compile("" + // Positive Lookahead
"(?=(.*\\d){2})" + // at least 2 numbers
"(?=.*[a-z])" + // small letters
"(?=.*[A-Z])" + // capital letters
"(?=.*[@$!%*?&#()+,-.:;<=>~^{|}_\\u0027 \\u005B \\u005D \\u0022 \\u002F \\u005C])" + // special characters and [ ] " / \
"[A-Za-z\\d$@$!%*?&#()+,-.:;<=>~^{|}_\\u0027 \\u005B \\u005B \\u0022 \\u002F \\u005C]" + // list of all allowed values
"{8,}");
答案 0 :(得分:2)
如果您只想要 Latin 字母和数字中的字母,您可以使用:
"[A-Za-z0-9]"
(假设你想要两种情况)
@Mena的建议更为紧凑,但这有点明确,并且不会让你想知道那个角色中有哪些角色。
更新其他条件:
最简单的方法是细分标准并单独检查:
// Check for minimum length.
if (password.length() < 8) {
return false;
}
// Check for at least one lower case letter.
if (!Pattern.compile("[a-z]").find(password)) {
return false;
}
// Check for at least one upper case letter.
if (!Pattern.compile("[A-Z]").find(password)) {
return false;
}
// etc, for other requirements.
尝试编写正则表达式以同时匹配所有这些标准将很难在将来进行更改。
答案 1 :(得分:1)
只需使用def some_function():
dict1 = {'key1':['1', '2'], 'key2':['3', '4']}
dict2 = {'key3':['5', '6'], 'key4':['7', '8']}
return (dict1, dict2)
def some_other_function(dict1, dict2):
# do something here with dict1 and dict2
# call some_function() and pass the results to some_other_function()
(d1,d2) = some_function()
some_other_function(d1,d2)
即可。
它将匹配字母数字,但不匹配非ASCII字符,符号,标点符号等。
示例强>
\\p{Alnum}
<强>输出强>
String[] test = {
"password123", // valid
"päaasword123", // invalid
"Passwörd9", // : invalid
};
Pattern p = Pattern.compile("\\p{Alnum}+");
for (String s: test) {
// matching against whole input here
System.out.println(p.matcher(s).matches());
}
答案 2 :(得分:0)
我找到了解决方案,单独检查每个约束,最后排除未知字符:
public boolean isValid(String value, ConstraintValidatorContext context) {
private static final Pattern PATTERN1 = Pattern.compile("[0-9]?[0-9]?(\\.[0-9][0-9]?)?"); // at least 2 digits
private static final Pattern PATTERN2 = Pattern.compile("[a-z]"); //lower case
private static final Pattern PATTERN3 = Pattern.compile("[A-Z]"); // upper case
private static final Pattern PATTERN4 = Pattern.compile("[@$!%*?&#()+,-.:;<=>~^{|}_\\u0027 \\u005B ]"); // special Matcher regMatcher1 = PATTERN1.matcher(value);
Matcher regMatcher2 = PATTERN2.matcher(value);
Matcher regMatcher3 = PATTERN3.matcher(value);
Matcher regMatcher4 = PATTERN4.matcher(value);
// Check for minimum length.
if (value.length() < 8) {
return false;
}
// at least 2 digits
if (!regMatcher1.find()) {
return false;
}
// Check for at least one lower case letter.
if (!regMatcher2.find()) {
return false;
}
// Check for at least one upper case letter.
if (!regMatcher3.find()) {
return false;
}
// at least 1 special character
if (!regMatcher4.find()) {
return false;
}
// doesn't contain anything besides the letters, digits and special characters
if (!value.matches("[a-zA-Z0-9!\"#$%&'()*+,-.?<=>@~^{|}_\\u0027 \\u005B]*")) {
return false;
}
无论如何,谢谢大家的帮助。干杯