正则表达式从4个条件中找出3个 [英] Regex to find 3 out of 4 conditions

查看:207
本文介绍了正则表达式从4个条件中找出3个的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的客户已要求其系统上的密码必须遵循一组特定的验证规则,而我很难提出一个很好的"正则表达式.

My client has requested that passwords on their system must following a specific set of validation rules, and I'm having great difficulty coming up with a "nice" regular expression.

我得到的规则是...

The rules I have been given are...

  • 最少8个字符
  • 允许任何字符
  • 至少必须有以下四个字符类型中的三个中的一个实例...
  • Minimum of 8 character
  • Allow any character
  • Must have at least one instance from three of the four following character types...
  1. 大写字母
  2. 小写字母
  3. 数字
  4. 特殊字符"

当我按下更多按钮时,特殊字符"实际上就是所有其他内容(包括空格).

When I pressed more, "Special Characters" are literally everything else (including spaces).

我可以使用以下命令轻松地检查所有四个实例中的至少一个实例...

I can easily check for at least one instance for all four, using the following...

^(?=.*?[A-Z])(?=.*?[a-z])(?=.*?\d)(?=.*?[^a-zA-Z0-9]).{8,}$

以下方法有效,但又可怕又混乱……

The following works, but it's horrible and messy...

^((?=.*?[A-Z])(?=.*?[a-z])(?=.*?\d)|(?=.*?[A-Z])(?=.*?[a-z])(?=.*?[^a-zA-Z0-9])|(?=.*?[A-Z])(?=.*?\d)(?=.*?[^a-zA-Z0-9])|(?=.*?[a-z])(?=.*?\d)(?=.*?[^a-zA-Z0-9])).{8,}$

因此您不必自己解决问题,上面正在检查(1,2,3|1,2,4|1,3,4|2,3,4),这是4组的4种可能组合(其中数字与规则集中的类型"相关).

So you don't have to work it out yourself, the above is checking for (1,2,3|1,2,4|1,3,4|2,3,4) which are the 4 possible combinations of the 4 groups (where the number relates to the "types" in the set of rules).

是否有更精细",更清洁或更简便的方法?

(请注意,这将在ASP.NET网站的<asp:RegularExpressionValidator>控件中使用,因此对于.NET和javascript都必须是有效的正则表达式.)

(Please note, this is going to be used in an <asp:RegularExpressionValidator> control in an ASP.NET website, so therefore needs to be a valid regex for both .NET and javascript.)

推荐答案

这不是更好的解决方案,但是您可以将[^a-zA-Z0-9]减小为[\W_],因为单词字符是所有字母,数字和下划线字符.我认为您无法在单个正则表达式中尝试进行轮换.我认为您几乎拥有最佳解决方案.

It's not much of a better solution, but you can reduce [^a-zA-Z0-9] to [\W_], since a word character is all letters, digits and the underscore character. I don't think you can avoid the alternation when trying to do this in a single regex. I think you have pretty much have the best solution.

一个小的优化是\d*[a-z]\w_*|\d*[A-Z]\w_*〜> \d*[a-zA-Z]\w_*,所以我可以删除其中一个替代集.如果您只允许4个中的3个不起作用,但是由于\d*[A-Z][a-z]\w_*被隐式允许,所以它可以工作.

One slight optimization is that \d*[a-z]\w_*|\d*[A-Z]\w_* ~> \d*[a-zA-Z]\w_*, so I could remove one of the alternation sets. If you only allowed 3 out of 4 this wouldn't work, but since \d*[A-Z][a-z]\w_* was implicitly allowed it works.

(?=.{8,})((?=.*\d)(?=.*[a-z])(?=.*[A-Z])|(?=.*\d)(?=.*[a-zA-Z])(?=.*[\W_])|(?=.*[a-z])(?=.*[A-Z])(?=.*[\W_])).*

扩展版本:

(?=.{8,})(
  (?=.*\d)(?=.*[a-z])(?=.*[A-Z])|
  (?=.*\d)(?=.*[a-zA-Z])(?=.*[\W_])|
  (?=.*[a-z])(?=.*[A-Z])(?=.*[\W_])
).*

由于OP指定了第四个条件,因此该正则表达式将匹配甚至不可打印的字符,例如换行符.如果这是不可接受的,则修改包含\W的集合以允许使用更特定的特殊字符集.

Because of the fourth condition specified by the OP, this regular expression will match even unprintable characters such as new lines. If this is unacceptable then modify the set that contains \W to allow for more specific set of special characters.

这篇关于正则表达式从4个条件中找出3个的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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