评估密码字符串的正则表达式
Regular expression to evaluate password string
我正在做一个小项目,我需要评估一个只有四个字符的字符串[我可以写一点 RE,但是这个让我明白了。]。
我需要编写一个正则表达式,它必须匹配 1 upper case
字、1 lower case
字、one digit
和一个随机字符,如 [a-zA-Z0-9]
。字符串中的顺序无关紧要。
这里有一些它应该通过或失败的案例字符串。
Valid words: Abn1, GGh3, 89jK….
Invalid words: abcd, 112a, abDb, 2Ab, 4, AA, ….
感谢任何帮助或提醒。
多重前瞻就是你的答案
\b(?=[a-zA-Z0-9]*[a-z])(?=[a-zA-Z0-9]*[A-Z])(?=[a-zA-Z0-9]*[0-9])[a-zA-Z0-9]{4}\b
(?=[a-zA-Z0-9]*[a-z]) # string contains any lowercase character
(?=[a-zA-Z0-9]*[A-Z]) # string contains any uppercase character
(?=[a-zA-Z0-9]*[0-9]) # string contains any digit
[a-zA-Z0-9]{4} # 4 characters, since the 4th is the type that can fit in any of the three
如果字符串来自单个输入(如 4 个字符的文本框,您应该将单词边界 (\b
) 替换为 ^
和 $
,例如
^(?=[a-zA-Z0-9]*[a-z])(?=[a-zA-Z0-9]*[A-Z])(?=[a-zA-Z0-9]*[0-9])[a-zA-Z0-9]{4}$
单个 RegExp 不是一个好方法。
在循环迭代字符中检查规则的最佳解决方案。
作为一种选择,您可以为每个规则编写 3 个简单的 Regexp,而不是一个大的 Regexp。
我正在做一个小项目,我需要评估一个只有四个字符的字符串[我可以写一点 RE,但是这个让我明白了。]。
我需要编写一个正则表达式,它必须匹配 1 upper case
字、1 lower case
字、one digit
和一个随机字符,如 [a-zA-Z0-9]
。字符串中的顺序无关紧要。
这里有一些它应该通过或失败的案例字符串。
Valid words: Abn1, GGh3, 89jK….
Invalid words: abcd, 112a, abDb, 2Ab, 4, AA, ….
感谢任何帮助或提醒。
多重前瞻就是你的答案
\b(?=[a-zA-Z0-9]*[a-z])(?=[a-zA-Z0-9]*[A-Z])(?=[a-zA-Z0-9]*[0-9])[a-zA-Z0-9]{4}\b
(?=[a-zA-Z0-9]*[a-z]) # string contains any lowercase character
(?=[a-zA-Z0-9]*[A-Z]) # string contains any uppercase character
(?=[a-zA-Z0-9]*[0-9]) # string contains any digit
[a-zA-Z0-9]{4} # 4 characters, since the 4th is the type that can fit in any of the three
如果字符串来自单个输入(如 4 个字符的文本框,您应该将单词边界 (\b
) 替换为 ^
和 $
,例如
^(?=[a-zA-Z0-9]*[a-z])(?=[a-zA-Z0-9]*[A-Z])(?=[a-zA-Z0-9]*[0-9])[a-zA-Z0-9]{4}$
单个 RegExp 不是一个好方法。
在循环迭代字符中检查规则的最佳解决方案。
作为一种选择,您可以为每个规则编写 3 个简单的 Regexp,而不是一个大的 Regexp。