By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,932 Members | 1,511 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,932 IT Pros & Developers. It's quick & easy.

Newbee question on regular expressions

P: n/a
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.
Nov 21 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a
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" wrote:
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.

Nov 21 '05 #2

P: n/a
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 [MVP VB]" wrote:
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" wrote:
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.

Nov 21 '05 #3

P: n/a
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" wrote:
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 [MVP VB]" wrote:
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" wrote:
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.

Nov 21 '05 #4

P: n/a
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 [MVP VB]" wrote:
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" wrote:
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.

Nov 21 '05 #5

P: n/a
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 [MVP VB]" wrote:
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" wrote:
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.

Nov 21 '05 #6

P: n/a
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 [MVP VB]" wrote:
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" wrote:
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.

Nov 21 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.