locked
I know, I know Simple Password Help RRS feed

  • Question

  • I read the sticky on Password strength. Mine is simple at least 2 upper case, at least 2 lower case, 2 numbers, which I have. What my issues are is that the first and last characters cannot be numbers. How can I incorporate that, this is what I have so far.

     ^[a-zA-Z]|[a-zA-Z]$|^(?=.*[A-Z]{2})(?=.*[a-z]{2})(?=.*[0-9]).*$


    v/r LikeToCode....Mark the best replies as answers.
    • Changed type LikeToCode Thursday, November 18, 2010 11:36 PM wasmarked as discussion
    Thursday, November 18, 2010 11:35 PM

Answers

  • ok... two uppercase:

    [A-Z].*[A-Z]

    two lower case:

    [a-z].*[a-z]

    two numbers:

    [0-9].*[0-9]

    No number at the start:

    ^[A-Za-z]

    No numbers at the end:

    [a-zA-Z]$

    combine:

    (?=.*[A-Z].*[A-Z])(?=.*[a-z].*[a-z])^[A-Za-z](?=[0-9].*[0-9]).*[a-zA-Z]$

    • Proposed as answer by Jesse HouwingMVP Friday, November 19, 2010 11:52 AM
    • Marked as answer by LikeToCode Sunday, November 21, 2010 9:47 PM
    Friday, November 19, 2010 10:04 AM

All replies

  • ok... two uppercase:

    [A-Z].*[A-Z]

    two lower case:

    [a-z].*[a-z]

    two numbers:

    [0-9].*[0-9]

    No number at the start:

    ^[A-Za-z]

    No numbers at the end:

    [a-zA-Z]$

    combine:

    (?=.*[A-Z].*[A-Z])(?=.*[a-z].*[a-z])^[A-Za-z](?=[0-9].*[0-9]).*[a-zA-Z]$

    • Proposed as answer by Jesse HouwingMVP Friday, November 19, 2010 11:52 AM
    • Marked as answer by LikeToCode Sunday, November 21, 2010 9:47 PM
    Friday, November 19, 2010 10:04 AM
  • Thanks for the reply, so you can specify anywhere in the pattern the ^ and the $? I thought they had to be in the same order the sting need to match. Any reason why you put the number match where you did? Could it be located anywhere in the pattern? Are you saying here .*[a-zA-Z]$ or (?=[0-9].*[0-9]).*, meaning match numbers to the end or only letters to the end? This is what I ended up with does this work the same ?

     (?=.*[A-Z]{2,})(?=.*[a-z]{2,})^[A-Za-z](?=.*[0-9]{2,}).*[a-zA-Z]$


    v/r LikeToCode....Mark the best replies as answers.
    Friday, November 19, 2010 7:54 PM
  • Your expression works differently.

     

    Firstly: (?=.*[A-Z]{2,}) looks for two consecutive capital letters, not two capital letters anywhere like mine dows.

    Secondly: the same goes for (?=.*[a-z]{2,})

    And third, that also is the case for your number expression.

    try:

    'Aa0a9A', which should match according to your specs, and won't work with your expression and will work with mine.

    The reason I places the number check in between the ^[A-Za-z] and the [A-Za-z]$ is because the first character could never be a number, so it was a a little optimization. This works exactly the same in the end:

    (?=.*[A-Z].*[A-Z])(?=.*[a-z].*[a-z])(?=[0-9].*[0-9])^[A-Za-z].*[a-zA-Z]$

    • Edited by Jesse HouwingMVP Saturday, November 20, 2010 11:09 AM copy/pasted the wrong expression
    Saturday, November 20, 2010 11:07 AM
  • Thank for the help Jesse. The pattern is not working in my script, it does not match anything. In the example below it should match "aB1c2De" but it is returning False on all of them. These words are the same as what I need to validate. The script is a vbs script. Thanks.

     

     

    Dim myArray, item
    
    myArray = Array("1abcDG1","Ab1c2b","111111","ABCDES","abcdes","Abc1dE","aB1c2De")
    
    For Each item In myArray
    	MsgBox item & " match is " & myRegEx(item)
    Next
    
    Function myRegEx(str)
    	Dim regex
    	Set regex = New RegExp
    	With regex
    		.Pattern = "(?=.*[A-Z].*[A-Z])(?=.*[a-z].*[a-z])(?=[0-9].*[0-9])^[A-Za-z].*[a-zA-Z]$"
    		.IgnoreCase = False
    		.Global = True
    	End With
    	myRegEx = regex.Test(str)
    End Function
    

    v/r LikeToCode....Mark the best replies as answers.
    Saturday, November 20, 2010 3:21 PM
  • Is that Classic VB? The Regex engine of VB is a lot less powerful then the (VB).NET engine is.

    I'm not sure if Look ahead assertions work that engine... If this is in VB.NET, then use the System.Text.RegularExpression.Regex.IsMatch() method, that should work.

    Saturday, November 20, 2010 9:05 PM
  • It is VBScript which looks a lot like old VB but it is for scripting and not complied apps. If I was writing an app I agree fully the .net Regular Expression engine is much better! I found the problem and it seems to be working now, you were missing the ".*" (?=[0-9].*[0-9]) so I changed it to (?=.*[0-9].*[0-9]) and it works as it should thanks! the completed pattern is

    (?=.*[A-Z].*[A-Z])(?=.*[a-z].*[a-z])^[A-Za-z](?=.*[0-9].*[0-9]).*[A-Za-z]$

    Thanks!


    v/r LikeToCode....Mark the best replies as answers.
    Sunday, November 21, 2010 9:47 PM