简体   繁体   English

2 个字母和 6 个数字的正则表达式

[英]Regular expression for 2 alphabets & 6 number

I have a password rule to be follow, required the password must contain 2 alphabet (upper or lowercase) & 6 numbers, and the whole length is expected to equal 8 symbols.我有一个密码规则要遵循,要求密码必须包含 2 个字母(大写或小写)和 6 个数字,并且整个长度预计等于 8 个符号。

Here's the samples that should pass:以下是应该通过的样本:

a123456b
1a2b3456
aa123456
123ab456

And the samples that should fail:以及应该失败的样本:

1abcdefg2
a1234567b
abcdefgh
12345678

And I need a regular expression to fit this rule.我需要一个正则表达式来符合这个规则。

Edit : TL;DR: Regex are not the tool for the job, and the job doesn't need to be done.编辑:TL;DR:正则表达式不是这项工作的工具,这项工作不需要完成。

Regex is not the solution to the problem, and AFAIK it is impossible to use it to solve your problem.正则表达式不是问题的解决方案,AFAIK 不可能用它来解决您的问题。 (But don't take my word for it, I bet someone wrote some funky extension to an esoteric Regex engine that does just this) (但不要相信我的话,我敢打赌有人为一个深奥的正则表达式引擎写了一些时髦的扩展,就是这样做的)

If it is possible, the solution will be unreadable.如果可能,解决方案将是不可读的。 It is better to just check the result of Count .最好只检查Count的结果。

Something along the lines of (haven't actually compiled this):类似的东西(实际上还没有编译这个):

bool IsPasswordValid(string pw)
{
    return pw.Length == 8 && 
            pw.Count(char.IsNumber) == 6 &&
            pw.Count(char.IsLetter) == 2;
}

Notes :注意事项

Regular Expressions are (is?) a tool that is used to check a string against a pattern.正则表达式是(是?)一种用于根据模式检查字符串的工具。 The simpler the pattern, the easier it is to write a regex for it.模式越简单,为它编写正则表达式就越容易。 Since you want your password to have 2-chars of a set anywhere in the string, and 6 character of another set also anywhere in the string, it is very hard (or even impossible) to create such a pattern.由于您希望您的密码在字符串中的任何位置具有一组 2 个字符,并且在字符串中的任何位置也具有另一组的 6 个字符,因此创建这样的模式非常困难(甚至不可能)。

If your problem was "I want the password to start with 2 digits and end with 6 letters", the answer would've been trivial, but since the digits and letters can be separate and anywhere in the string, it is not as trivial.如果您的问题是“我希望密码以 2 位数字开头并以 6 个字母结尾”,那么答案将是微不足道的,但由于数字和字母可以分开并位于字符串中的任何位置,因此它并不那么微不足道。

Also, most of the time, enforcing password patterns does not increase security.此外,大多数情况下,强制执行密码模式并不能提高安全性。 If you have to do anything, enforce a sane minimum length ans stop there.如果您必须做任何事情,请强制执行合理的最小长度并就此停止。

A regex approach can be multiple, here are 2: 1) require the legnth of 8 chars with a lookahead like (?=.{8}$) and use a consuming pattern allowing zero or more digits before a letter twice, and then match zero or more letters, 2) match 8 letters or digits, but use a lookahead restriction to match 2 alphabets.正则表达式方法可以有多种,这里有 2 种:1) 需要 8 个字符的长度,并使用类似(?=.{8}$)的前瞻,并使用消费模式,允许在两次字母前有零个或多个数字,然后匹配零个或多个字母,2) 匹配 8 个字母或数字,但使用先行限制来匹配 2 个字母。

Example 1:示例 1:

^(?=.{8}$)(?:\d*[a-z]){2}\d*$

Details :详情

  • ^ - start of string ^ - 字符串的开始
  • (?=.{8}$) - the length should be 8 non-newline symbols only (?=.{8}$) - 长度只能是 8 个非换行符
  • (?:\\d*[az]){2} - 2 sequences of 0+ digits followed with 1 letter (?:\\d*[az]){2} - 2 个 0+ 数字序列,后跟 1 个字母
  • \\d* - 0+ digits \\d* - 0+ 位数
  • $ - end of string. $ - 字符串的结尾。

See the regex demo查看正则表达式演示

Example 2:示例 2:

^(?=(?:\d*[a-z]){2}\d*$)[\da-z]{8}$

Details :详情

  • ^ - start of string ^ - 字符串的开始
  • (?=(?:\\d*[az]){2}\\d*$) - there must be 2 sequences of 0+ digits and a letter followed with 0+ digits up to the end of string (?=(?:\\d*[az]){2}\\d*$) - 必须有 2 个 0+ 数字序列和一个字母后跟 0+ 数字直到字符串末尾
  • [\\da-z]{8} - 8 digits or letters [\\da-z]{8} - 8 位数字或字母
  • $ - end of string. $ - 字符串的结尾。

See the regex demo 2参见正则表达式演示 2

Note that to enable case insensitive mode, you can prepend the pattern with (?i) / RegexOptions.IgnoreCase flag, and if you need to only match ASCII digits, use RegexOptions.ECMAScript or replace \\d with [0-9] .请注意,要启用不区分大小写的模式,您可以在模式前加上(?i) / RegexOptions.IgnoreCase标志,如果您只需要匹配 ASCII 数字,请使用RegexOptions.ECMAScript或将\\d替换为[0-9]

Actually,there is a nice way to do it:实际上,有一个很好的方法来做到这一点:

    public static bool CheckPasswordRule(string password)
    {
        var isRuleAdhered = (Regex.Matches(password, "\\d").Count == 6 && Regex.Matches(password, "[a-z]").Count == 2);

        return isRuleAdhered;
    }

If you still want to not have 6 digits one after another you can modify the code to:如果您仍然不想一个接一个地有6位数字,您可以将代码修改为:

    public static bool CheckPasswordRule(string password)
    {
        var isRuleAdhered= false;
        isRuleAdhered = Regex.Matches(password, "\\d").Count == 6 && Regex.Matches(password, "[a-z]").Count == 2 && !Regex.IsMatch(password, "\\d{6}");
        return isRuleAdhered;
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM