0

I am looking for a regular expression to validate the input which should contains at least three of the following four characters group:

English uppercase characters (A through Z)
English lowercase characters (a through z)
Numerals (0 through 9)
Non-alphabetic characters (such as !, $, #, %)

Thanks in advance.

Edit: This is for .NET Framework

1
  • @Brian Just edited the question to include you answer Commented Jul 14, 2009 at 11:34

2 Answers 2

4

Not in one regex, but think this is the way:

int matchedGroupCount = 0;
matchedGroupCount += Regex.IsMatch(input, "[a-z]") ? 1 : 0;
matchedGroupCount += Regex.IsMatch(input, "[A-Z]") ? 1 : 0;
matchedGroupCount += Regex.IsMatch(input, "[0-9]") ? 1 : 0;
matchedGroupCount += Regex.IsMatch(input, "[!*#%, etc..]") ? 1 : 0;

if (matchedGroupCount >= 3)
   pass
else
   failed
Sign up to request clarification or add additional context in comments.

1 Comment

Guess you should use matchedGroupCount >= 3 instead of equality sign.
0

I can't think of a direct way to do this to be honest: regular expressions aren't very supportive of "must contain". What language are you writing this in? Personally, I'd do this by checking for each regular expression in turn and counting how many matches you get, so in python it would be something like this:

#!/usr/bin/python
import re
count = 0
mystring = "password"
regexp = re.compile(r'[A-Z]')
if regexp.search(mystring) is not None:
    count += 1
regexp = re.compile(r'[a-z]')
if regexp.search(mystring) is not None:
    count += 1
# etc
if count < 3:
    print "Not enough character types"

You could also do this a bit more cleanly with:

#!/usr/bin/python
import re
regexpArray = [re.compile(r'[A-Z]'), re.compile(r'[a-z]'), re.compile(r'\d'), re.compile(r'[^A-Za-z0-9]')]
count = 0
for regexp in regexpArray:
    if regexp.search(mystring) is not None:
        count += 1
if count < 3:
    print "Not enough character types"

Alternatively, you could have a very complicated regular expression with lots of options (in different orders) or one of the various password strength checkers that you'll be able to find with google.

Edit:

The python way to do this without regular expressions would be something like the following. I'm sure there's a .NET equivalent for this that would be much quicker than regular expression matching.

#!/usr/bin/python
import string

mystring = "password"
count = 0
for CharacterSet in [string.ascii_lowercase, string.ascii_uppercase, "0123456789", r'''!"£$%^&*()_+-=[]{};:'@#~,<.>/?\|''']:
    # The following line adds 1 to count if there are any instances of
    # any of the characters in CharacterSet present in mystring
    count += 1 in [c in mystring for c in CharacterSet]
if count < 3:
    print "Not enough character types"

There may be a better way of generating the list of symbols.

3 Comments

if in Python, i would suggest str.isalpha(), len(password)==3, str.isdigit() instead of using re.
I was trying to remember some good "filter" style ways of doing it, but note that isalpha() will return False if there are ANY non-alphanumeric characters, which isn't what is needed here. What is needed is something like "if any of ['a', 'b', ...] in mystring": count += 1".
I've added an alternative that avoids the regular expression.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.