密码规则的正则表达式 [英] Regex expression for password rules

查看:64
本文介绍了密码规则的正则表达式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对密码规则有要求.以下是规则.

I have a requirement for password rules. Following are the rules.

密码必须遵循以下准则:

The password must follow the following guidelines:

  • 长度至少为 8 个字符
  • 包含这 4 个选项中的 3 个:小写字母、大写字母、数字或特殊字符
  • 当用户指定的密码不符合上述规则时,返回消息说明:

  • Be at least eight characters long
  • Contain 3 of these 4 options: lower case letter, upper case letter, number, or special character
  • When user specifies a password that does not meet the above rules, return message stating:

密码长度必须至少为 8 个字符,并包含以下 4 个选项中的 3 个:

Password must be at least 8 characters long and contain 3 of the 4 following options:

  • 小写字母 (a-z)
  • 大写字母 (A-Z)
  • 数字 (0-9)
  • 特殊字符 (!@#$%^&')

请帮我得到一个正则表达式来处理上述条件.

Please help me to get a regex expression to handle above conditions.

感谢您的所有帮助.以下是我的要求的解决方案

i appreciate all your help. following is the solution for my requirement

if(password.matches("^(?=.*[0-9]).{1,}$")){
validCount++;
}
if(password.matches("^(?=.*[a-z]).{1,}$")){
validCount++;
}
if(password.matches("^(?=.*[A-Z]).{1,}$")){
validCount++;
}
if(password.matches("^(?=.*[@#$%^&+=]).{1,}$")){
validCount++;
}
return validCount >= 3 ? true : false;

谢谢,拉姆基

推荐答案

这是,如果你想要一个优雅的正则表达式,尽可能接近

This is, if you want an elegant regex, as close as you can get

^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[!@#$%^&'])[^ ]{8,}$

基本思想是使用一种称为正向前瞻"的技术:

The basic idea is to use a technique called "positive lookahead" :

(?=.*PutHereWhatYouWantToAllow)

您的额外要求 4 中的 3 不容易用正则表达式解决,因为您基本上无法使它们计数.您可以写出上述正则表达式的必要排列(告诉我它是否没有意义),但这会使正则表达式非常长.您可以做的是在代码中写出排列,以便正则表达式保持可维护性,因为您不会从字面上重复这些模式.

Your extra requirement 3 out of 4 is not easy to solve with regexes cause you cannot make them count basically. You could write out the necessary permutations of the above regex (tell me if it doesn't make sense) but that would make a very long regex. What you could do is write out the permutations in code so that the regex stays maintainable since you are not repeating the patterns literally.

如果你告诉我你的语言(C#?),我会试一试,因为这是一个很好的挑战.

I'll have a shot if I you tell me your language (C#?) cause it's a good challenge.

更新 1

这里的正则表达式至少可以满足您的 3 个要求(也允许 4 个),只是为了挑战它.不要在生产中使用它,而是使用评论中提到的单个正则表达式在语言中循环.

Here is the regex that will match at least 3 of your requirements (4 is also allowed), just for the challenge of it. Don't use this in production but loop in the language with individual regexes as mentioned in the comments.

^((?=.[az].[AZ].[\d])|(?=.[az].[\d].[AZ])|(?=.[AZ].[az].[\d])|(?=.[AZ]].[\d].[az])|(?=.[\d].[az].[AZ])|(?=.[\d].[AZ].[az])|(?=.[az].[AZ].[!@#$​​%^&'])|(?=.[az].[!@#$%^&'].[AZ])|(?=.[AZ].[az].[!@#$%^&'])|(?=.[AZ].[!@#$%^&'].[az])|(?=.[!@#$%^&'].[az].[AZ])|(?=.[!@#$%^&'].[AZ].[az])|(?=.[az].[\d].[!@#$%^&'])|(?=.[az].[!@#$%^&'].[\d])|(?=.[\d].[az].[!@#$%^&'])|(?=.[\d].[!@#$%^&'].[az])|(?=.[!@#$%^&'].[az].[\d])|(?=.[!@#$%^&'].[\d].[az])|(?=.[AZ].[\d].[!@#$%^&'])|(?=.em>[AZ].[!@#$%^&'].[\d])|(?=.[\d].[AZ].[!@#$%^&'])|(?=.[\d].[!@#$%^&'].[AZ])|(?=.[!@#$%^&'].[AZ].[\d])|(?=.[!@#$​​%^&'].[\d].[A-Z]))[^ ]{8,}$

^((?=.[a-z].[A-Z].[\d])|(?=.[a-z].[\d].[A-Z])|(?=.[A-Z].[a-z].[\d])|(?=.[A-Z].[\d].[a-z])|(?=.[\d].[a-z].[A-Z])|(?=.[\d].[A-Z].[a-z])|(?=.[a-z].[A-Z].[!@#$%^&'])|(?=.[a-z].[!@#$%^&'].[A-Z])|(?=.[A-Z].[a-z].[!@#$%^&'])|(?=.[A-Z].[!@#$%^&'].[a-z])|(?=.[!@#$%^&'].[a-z].[A-Z])|(?=.[!@#$%^&'].[A-Z].[a-z])|(?=.[a-z].[\d].[!@#$%^&'])|(?=.[a-z].[!@#$%^&'].[\d])|(?=.[\d].[a-z].[!@#$%^&'])|(?=.[\d].[!@#$%^&'].[a-z])|(?=.[!@#$%^&'].[a-z].[\d])|(?=.[!@#$%^&'].[\d].[a-z])|(?=.[A-Z].[\d].[!@#$%^&'])|(?=.[A-Z].[!@#$%^&'].[\d])|(?=.[\d].[A-Z].[!@#$%^&'])|(?=.[\d].[!@#$%^&'].[A-Z])|(?=.[!@#$%^&'].[A-Z].[\d])|(?=.[!@#$%^&'].[\d].[A-Z]))[^ ]{8,}$

更新 2

这是在java中采用的方法

This is the approach to take in java

根据我读到的评论,您正在测试如下

From the comments I read that you are testing like the following

  • 小写"^[a-z]*$";
  • 大写^[A-Z]*$";
  • digits="^[0-9]*$";

我不认为你在这里走在正确的轨道上.如果所有字符都是小写,而不是一个,小写只会报告成功.其余的同理.

I don't think you are on the right track here. The lowercase will only report success if all characters are lowercase, and not just one. Same remark for the rest.

这些是 4 个单独的正则表达式,其中至少有 3 个应该报告匹配

These are the 4 individual regexes of which at least 3 should report a match

[a-z]
[A-Z]
\d
[!@#$%^&']

这里测试密码不能包含空格

Here is the test that the password should not contain a space

^[^ ]*$

测试至少8个字符

.{8,}

所以我拆分了需求而不是合并它们.这应该使代码更具可读性,尤其是当代码以正则表达式开头时.

So I split the requirements and not combine them. This should make for more readable code especially if one starts with regexes.

这篇关于密码规则的正则表达式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆