简体   繁体   English

正则表达式验证问题

[英]Regular Expression validation Issues

I'm using the 我正在使用

^(?=^.{14,30}$)(?=^.[\d]{3}$)(?=^.[a-z]{4}$)(?=^.[A-Z]{5}$)(?=^.[!*&#$%^]{2}).$

regular expression in order to validate the following as minimum requirements: 正则表达式以验证以下最低要求:

  1. Four low case letter 四个小写字母
  2. Five Upper case letters 五个大写字母
  3. Three digits 三位数
  4. Two special characters 两个特殊字符
  5. With total length between 14 to 30 characters long 总长度介于14到30个字符之间

But Unfortunately This validation pattern doesn't validate my string. 但是不幸的是,这种验证模式无法验证我的字符串。
I've look for regex pattern validation and gives me that the pattern is OK 我正在寻找regex模式验证,并告诉我该模式还可以
Can someone give me a support on this? 有人可以给我支持吗?

Note that (?=^.[az]{4}$) requires a match of a 5 char string where the first one csn be any char and then there must be 4 lowercase letters. 请注意, (?=^.[az]{4}$)需要匹配5个字符的字符串,其中第一个csn是任何字符,然后必须有4个小写字母。 In the end, after ^ and lookaheads, there is . 最后,在^之后,再往前看. before $ , so actually, the whole regex can match a single char string. $之前,因此实际上,整个正则表达式可以匹配单个char字符串。

You may use 您可以使用

^(?=(?:\D*\d){3})(?=(?:[^a-z]*[a-z]){4})(?=(?:[^A-Z]*[A-Z]){5})(?=(?:[^!*&#$%^]*[!*&#$%^]){2}).{8,30}$

See the regex demo . 参见regex演示 A regulex graph : 一个regulex图

在此处输入图片说明

Details 细节

  • ^ - start of string ^ -字符串的开头
  • (?=(?:\\D*\\d){3}) - three occurrences of any 0+ non-digits followed with a digit (?=(?:\\D*\\d){3}) -出现3次任何0+个非数字,后跟一个数字
  • (?=(?:[^az]*[az]){4}) - four occurrences of any 0+ chars other than a lowercase ASCII letters followed with a lowercase ASCII letter (?=(?:[^az]*[az]){4}) -出现四次任意0+字符,除了小写ASCII字母后跟小写ASCII字母
  • (?=(?:[^AZ]*[AZ]){5}) - five occurrences of any 0+ chars other than an uppercase ASCII letters followed with an uppercase ASCII letter (?=(?:[^AZ]*[AZ]){5}) -除大写ASCII字母后跟大写ASCII字母外,其他任何0+字符均出现五次
  • (?=(?:[^!*&#$%^]*[!*&#$%^]){2}) - two occurrences of any 0+ chars other than some specific special chars (defined in the character class) followed with a char from the sepcified !*&#$%^ set (?=(?:[^!*&#$%^]*[!*&#$%^]){2}) -两次出现任何0+字符,而不是某些特定的特殊字符(在字符中定义)类),然后是来自分隔的!*&#$%^集的一个字符
  • .{8,30} - any 8 to 30 chars .{8,30} -任意8到30个字符
  • $ - end of string. $ -字符串结尾。
^(?=.{14,30}$)(?=(?:.*[A-Z]){5})(?=(?:.*[a-z]){4})(?=(?:.*\d){3})(?=(?:.*[!*&#$%^]){2}).*$
 └─────┬─────┘└───────┬────────┘└───────┬────────┘└──────┬──────┘└─────────┬──────────┘
       │              │                 │                │                 │
       │              │                 │                │        2 special characters
       │              │                 │                │
       │              │                 │            3 digits
       │              │                 │
       │              │          4 lowercase letters
       │              │ 
       │       5 uppercase letters
       │
string is 14-30 characters long 

^(?=(?:.*[A-Z]){5})(?=(?:.*[a-z]){4})(?=(?:.*\d){3})(?=(?:.*[!*&#$%^]){2}).(14,30)$
 └───────┬────────┘└───────┬────────┘└──────┬──────┘└─────────┬──────────┘
         │                 │                │                 │
         │                 │                │        2 special characters
         │                 │                │
         │                 │            3 digits
         │                 │
         │          4 lowercase letters
         │ 
  5 uppercase letters

Instead of a regex, which may be difficult to maintain, how about some simple code that is easy to read and modify: 用一些易于阅读和修改的简单代码代替正则表达式(可能很难维护):

Private Function IsStringValid(s As String) As Boolean
    If (s.Length > 30) OrElse (s.Length < 14) Then
        Return False
    End If

    Dim lower, upper, digit, special As Integer
    For Each c As Char In s
        Select Case True
            Case Char.IsLower(c)
                lower += 1
            Case Char.IsUpper(c)
                upper += 1
            Case Char.IsDigit(c)
                digit += 1
            Case Char.IsSymbol(c) OrElse Char.IsPunctuation(c)
                special += 1
        End Select
    Next
    Return (lower >= 4) AndAlso (upper >= 5) AndAlso (digit >= 3) AndAlso (special >= 2)
End Function

Based on your requirements, the minimum length has to be 14. 根据您的要求,最小长度必须为14。

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

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