Need a regular expression

guest_rg

Newcomer
Joined
Dec 22, 2004
Messages
3
I am new to regular expressions and I need a regular expression for an important task. The requirement is for a password string with:

8 to 20 chars long
2 chars must be capitals, special characters or numbers

Thanks in advance
 
guest_rg said:
I am new to regular expressions and I need a regular expression for an important task. The requirement is for a password string with:

8 to 20 chars long
2 chars must be capitals, special characters or numbers

Thanks in advance

I believe you will have to verify the length using .NET's String.Length, but you can check for the other stuff using something like this:

[0-9A-Z!-@].*[0-9A-Z!-@]

If you don't find the above string in your password then it fails the character test. This regular expression amounts to the following:

Find an occurrence of a digit, capital letter, or special symbol followed by anything followed by another digit, capital letter, or special symbol. In other words, find at least one of your desired characters followed eventually by another one.

There may be other ways using :digit: and other identifiers, but the above method is one I use for tasks like yours. The square brackets [] are used to represent the occurrence of one (1) character that is one of the characters in the list inside the square brackets. If you wanted to negate the search you would use something like:

[^A-Za-b]

which means no alphabetic characters. The caret ^ symbol at the beginning of the bracketed list means "none of these".

Hope this helps. :)
 
Richard,

This is helpful but I was hoping that there would be a way to perform all the checks in one expression. The password format check is going to be database driven. I want it to be dynamic. If I make the length check using String.Length, I will have to make coding changes if the requirement changes.

Everybody, Please let me know if there is a answer to this.

Thanks


Richard Crist said:
I believe you will have to verify the length using .NET's String.Length, but you can check for the other stuff using something like this:

[0-9A-Z!-@].*[0-9A-Z!-@]

If you don't find the above string in your password then it fails the character test. This regular expression amounts to the following:

Find an occurrence of a digit, capital letter, or special symbol followed by anything followed by another digit, capital letter, or special symbol. In other words, find at least one of your desired characters followed eventually by another one.

There may be other ways using :digit: and other identifiers, but the above method is one I use for tasks like yours. The square brackets [] are used to represent the occurrence of one (1) character that is one of the characters in the list inside the square brackets. If you wanted to negate the search you would use something like:

[^A-Za-b]

which means no alphabetic characters. The caret ^ symbol at the beginning of the bracketed list means "none of these".

Hope this helps. :)
 
Here's what we do at my work

guest_rg said:
Richard,

This is helpful but I was hoping that there would be a way to perform all the checks in one expression. The password format check is going to be database driven. I want it to be dynamic. If I make the length check using String.Length, I will have to make coding changes if the requirement changes.

Everybody, Please let me know if there is a answer to this.

Thanks

At my work we do exactly what you are wanting to do. We have database-driven regular expressions to check data values. Our database design and implementation has separate fields for min/max length. That is, the min/max lengths are in two fields and the regular expression is in another. We could once and then the database can be modified to specify data lengths and content. The reason is because there is not much of a way for a singe regular expression to specify variable length and certain desired or undesired data content in the same expression.

To check for length you could use something like:

.{2,80}

which would mean any character for a minimum of 2 times to a maximum of 80 times. But it is hard to combine that kind of check with an additional data content check where the data pattern to be checked can be anywhere in the string.

I hope this makes sense. Perhaps someone else could elaborate or provide a single regular expression that can do what we are discussing? :confused:
 
Well...then I will check for the rules in multiple RegExp's. Please tell me if these individual RegExp's are correct.

-- String that is 8 to 20 chars long.
^(?=.*).{8,20}$

-- 2 chars must be capitals, special characters or numbers. In other words, this can be interpreted as the string contains atleast 2 chars that are not small letters.
^(?=[^a-z].*[^a-z])$

Thanks
 
Looks good

guest_rg said:
Well...then I will check for the rules in multiple RegExp's. Please tell me if these individual RegExp's are correct.

-- String that is 8 to 20 chars long.
^(?=.*).{8,20}$

-- 2 chars must be capitals, special characters or numbers. In other words, this can be interpreted as the string contains atleast 2 chars that are not small letters.
^(?=[^a-z].*[^a-z])$

Thanks

Well....the expression

^.{8,20}$

will pick up expressions 8 to 20 characters long between the start and end of the string being tested. If this test passes then the length is correct.

And the expression

^.*[^a-z].*[^a-z].*$

will pick up expressions that have at least two non-lowercase characters somewhere in the string. If this test passes then the data content is correct.

So, your regular expressions are correct, but I'm not sure what the ? and = characters are for in your regular expressions above. Are they the only special characters allowed? Other than that I believe you've got a scenario that should work. :)
 
Back
Top