Regular Expression Help

G

Guest

I've been looking at regular expression examples and can't seem to find what
I am looking to do. Is there a regular expression that can test the following?

1. Contain at least 1 lowercase letter.
2. Contain at least 1 uppercase letter.
3. Contain at least 1 number
4. Contain at least 1 special character (i.e. *, &, %, #, etc).
5. Be at least 8 characters in length
 
G

Guest

A regexp is overkill for your needs - they're expensive and this is simpler
to accomplish with a loop:

if (str.Length < 8)
{
// not valid
}

int bitmap = 0; // record each requirement being met in the first 4 bits

foreach (char ch in str)
{
if (Char.IsLower(ch)) bitmap |= 1;
else if (Char.IsUpper(ch)) bitmap |= 2;
else if (Char.IsDigit(ch)) bitmap |= 4;
else if (ch == '*' || ch == '%' || ch == (other chars) ) bitmap |= 8;

if (bitmap >= (1 | 2 | 4 | 8)) break;
}

if (bitmap >= (1 | 2 | 4 | 8))
{
// valid
}


KH
 
G

Guest

Thanks...I appreciate you pointing out that RegEx was too much overhead for
what I wanted to do. The solution you gave works great...thanks again.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Top