Newbee question on regular expressions

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hello:

I am trying to learn about regular expressions.

How would I use regular expressions to ensure that the password an user
enters conforms to the following rules:

1. It has to be between 3-20 characters long.
2. Only a-z, A-Z, 0-9, +-_ are allowed.
3. At least one occurence of a lower case letter, a upper case letter and a
digit has to be used.

Any help will be appreciated.
 
Hi wenk,

try the next regular expression:
^([a-zA-Z0-9|\+|\-\(_)]{3,20})$

(test it first please)

I hope that helps.

Kind Regards,

Jorge Serrano
MVP VB.NET
 
Hi vvenk

Jorge's RegEx is a good start - it satisfies rules 1 and 2 - but it would
allow 'aaa' as a valid input for example.

You could use this regular expression followed by a second test against
[0-9], a third test against [A-Z], and a fourth against [a-z], to satisfy
rule number 3.

Nigel

Jorge Serrano said:
Hi wenk,

try the next regular expression:
^([a-zA-Z0-9|\+|\-\(_)]{3,20})$

(test it first please)

I hope that helps.

Kind Regards,

Jorge Serrano
MVP VB.NET


vvenk said:
Hello:

I am trying to learn about regular expressions.

How would I use regular expressions to ensure that the password an user
enters conforms to the following rules:

1. It has to be between 3-20 characters long.
2. Only a-z, A-Z, 0-9, +-_ are allowed.
3. At least one occurence of a lower case letter, a upper case letter and a
digit has to be used.

Any help will be appreciated.
 
Nigel:

Thank you.

Here's my subroutine that tests the password using the regular expression
that Jorge had suggested:

Private sub test_password
Dim lsPassword As String
Dim lsPattern As String = "^([a-zA-Z0-9|\+|\-\(_)]{3,20})$"
lsPassword = "aa"
Dim mc As MatchCollection = Regex.Matches(lsPassword, lsPassword)
Dim m As Match

For Each m In mc
MsgBox(m.ToString)
Next
End Sub

However, when I test it, mc.count is 1 when I expect it to be 0. So, it
breaks Rule 1. The only rule this enforces is Rule 2.

Of cource, your suggestion addresses enforcing Rule 3.

Thanks.

Nigel Armstrong said:
Hi vvenk

Jorge's RegEx is a good start - it satisfies rules 1 and 2 - but it would
allow 'aaa' as a valid input for example.

You could use this regular expression followed by a second test against
[0-9], a third test against [A-Z], and a fourth against [a-z], to satisfy
rule number 3.

Nigel

Jorge Serrano said:
Hi wenk,

try the next regular expression:
^([a-zA-Z0-9|\+|\-\(_)]{3,20})$

(test it first please)

I hope that helps.

Kind Regards,

Jorge Serrano
MVP VB.NET


vvenk said:
Hello:

I am trying to learn about regular expressions.

How would I use regular expressions to ensure that the password an user
enters conforms to the following rules:

1. It has to be between 3-20 characters long.
2. Only a-z, A-Z, 0-9, +-_ are allowed.
3. At least one occurence of a lower case letter, a upper case letter and a
digit has to be used.

Any help will be appreciated.
 
Jorge:

Thanks for your help. Can you clarify the following questions?

1. I understand the use of "|"; it's an "or". I see that you are saying
"(any alpha or number)" or "+" or "-". I don't understand "\(_)". Why can't I
say \_. What is the significance of the open and close paranthesis?

2. Also, this does not seem to enforce Rule 1 concering the minimum and the
maximum length.

Thanks.

Venkat

Jorge Serrano said:
Hi wenk,

try the next regular expression:
^([a-zA-Z0-9|\+|\-\(_)]{3,20})$

(test it first please)

I hope that helps.

Kind Regards,

Jorge Serrano
MVP VB.NET


vvenk said:
Hello:

I am trying to learn about regular expressions.

How would I use regular expressions to ensure that the password an user
enters conforms to the following rules:

1. It has to be between 3-20 characters long.
2. Only a-z, A-Z, 0-9, +-_ are allowed.
3. At least one occurence of a lower case letter, a upper case letter and a
digit has to be used.

Any help will be appreciated.
 
Jorge:

Sorry, my apologies. I was used the same variable for both the pattern and
the string.

Thanks to you and Nigel, I am a happy camper.

Venkat

Jorge Serrano said:
Hi wenk,

try the next regular expression:
^([a-zA-Z0-9|\+|\-\(_)]{3,20})$

(test it first please)

I hope that helps.

Kind Regards,

Jorge Serrano
MVP VB.NET


vvenk said:
Hello:

I am trying to learn about regular expressions.

How would I use regular expressions to ensure that the password an user
enters conforms to the following rules:

1. It has to be between 3-20 characters long.
2. Only a-z, A-Z, 0-9, +-_ are allowed.
3. At least one occurence of a lower case letter, a upper case letter and a
digit has to be used.

Any help will be appreciated.
 
Back
Top