密码检查正则表达式匹配多个条件 [英] Password checking RegEx that matches multiple criteria

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

问题描述

广泛的搜索在谷歌和SO后,我遇到了一个问题。我想写基于关闭一个正则表达式我发现SO

After searching extensively on Google and SO I ran into an issue. I am trying to write a regex based off one I found on SO

  1. 该字符串必须是8-24个字符,以及

  1. The string must be 8-24 characters, and

的字符串需要匹配两个以下标准(最小),在除了长度:

The string needs to match two of the following criteria (minimally), in addition to the length:

  • 在大写
  • 小写
  • 数字
  • 非字母数字

这是正则表达式我一直在试图修改:

This is the regex I have been trying to modify:

^.*(?=.{8,24})(?=.*\d|\W)(?=.*[a-zA-Z]).*$

我需要修改,​​这是确保密码符合上面的标准。

I need to modify this is to ensure a password matches the criteria above.

我不能去和编程方式做到这一点,因为这将改变怎么我们的一些code ++工程等基础知识。现在,我们已经制定了正则表达式检查,以便所有我必须做的是更新与新的前pression一个配置文件。

I cannot go in and programmatically do this because it would change other fundamentals of how some of our code works. Right now we have the regex check in place so all I must do is update a config file with the new expression.

推荐答案

没有它不是pretty的,但你的可以的有效使用蛮力像这样用一个正则表达式做到这一点(假设C# ):

No its not pretty, but you can efficiently do it with a single regex using brute force like so (assuming C#):

Regex re = new Regex(@"
    # Match 2 of 4 passwords criteria and length from 8 to 24.
    ^                   # Anchor to start of string.
    (?:                 # Group acceptable pair alternatives.
      (?=[^A-Z]*[A-Z])  # At least one Upper Case.
      (?=[^a-z]*[a-z])  # At least one Lower Case.
    |                   # or...
      (?=[^A-Z]*[A-Z])  # At least one Upper Case.
      (?=[^0-9]*[0-9])  # At least one Numeric.
    |                   # or...
      (?=[^A-Z]*[A-Z])  # At least one Upper Case.
      (?=\w*\W)         # At least one Non-AlphaNumeric.
    |                   # or...
      (?=[^a-z]*[a-z])  # At least one Lower Case.
      (?=[^0-9]*[0-9])  # At least one Numeric.
    |                   # or...
      (?=[^a-z]*[a-z])  # At least one Lower Case.
      (?=\w*\W)         # At least one Non-AlphaNumeric.
    |                   # or...
      (?=[^0-9]*[0-9])  # At least one Numeric.
      (?=\w*\W)         # At least one Non-AlphaNumeric.
    )                   # Brute force!
    .{8,24}             # Match from 8 to 24 chars.
    \z                  # Anchor to end of string.
    ", RegexOptions.IgnorePatternWhitespace);
if (re.IsMatch(text)) {
    // Password is valid.
} else {
    // Password is NOT valid.
} 

有的2出4要求六种可能的组合。最后 {8,24} 长度检查假定除换行符以外的任何字符就可以了(你可以/应该要修改此)。

There are six possible combinations of the 2 out of 4 requirements. The final .{8,24} length check assumes that any char other than a newline is ok (you may/should want to modify this).

编辑:我看到现在dbaupp的回答只是正常(我把它我upvote)。虽然我的前pression找到一个大写字母: 更有效率比(= [^ AZ] * [AZ]?):( ?=。* [AZ])(和同样适用于其他向前看符号)。另一方面,dbaupp的答案是关于分组更高效

I see now that dbaupp's answer works just fine (I gave it my upvote). Although my expression to find an uppercase letter: (?=[^A-Z]*[A-Z]) is more efficient than: (?=.*[A-Z]) (and the same goes for the other lookaheads). On the other hand, dbaupp's answer is more efficient with regard to the grouping.

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

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