Need RegEx expression for Strong passwords

Any questions involving matching text strings to patterns - the pattern is called a "regular expression."

Moderator: General Moderators

Post Reply
alicho
Forum Newbie
Posts: 2
Joined: Wed Sep 08, 2010 4:08 pm

Need RegEx expression for Strong passwords

Post by alicho »

Hi

I need RegEx expression for the following:

Passwords must be at least eight characters including at least one number and one special character (punctuation, symbol or upper/lower case character).

Note: The RegEx will be used in .NET



Thanks much!
User avatar
ridgerunner
Forum Contributor
Posts: 214
Joined: Sun Jul 05, 2009 10:39 pm
Location: SLC, UT

Re: Need RegEx expression for Strong passwords

Post by ridgerunner »

Not sure which .NET tool you are using. Here's a C# snippet that might do the trick...

Code: Select all

bool foundMatch = false;
try {
    foundMatch = Regex.IsMatch(subjectString, 
        @"\A(?:^             (?# Anchor to beginning of string.)
        (?=\S*?[0-9])        (?# At least one number.)
        (?=\S*?[A-Z])        (?# At least one uppercase letter.)
        (?=\S*?              (?# At least one punctuation, symbol, or)
          [~!@#$%^&*()_+-={}[\]|:;<>,.?/'"`a-z] (?# lowercase letter.)
        ) \S{8,}             (?# Match 8 or more chars.)
        $                    (?# Anchor to end of string.)
        )\Z", 
        RegexOptions.IgnorePatternWhitespace);
} catch (ArgumentException ex) {
    // Syntax error in the regular expression
}
In RAW regex syntax:

Code: Select all

^(?=\S*?[0-9])(?=\S*?[A-Z])(?=\S*?[~!@#$%^&*()_+-={}[\]|:;<>,.?/'"`a-z])\S{8,}$
It must be at least 8 chars long, requires at least one number, at least one uppercase letter, and at least one punctuation, symbol, or lowercase letter, and must contain no whitespace.
alicho
Forum Newbie
Posts: 2
Joined: Wed Sep 08, 2010 4:08 pm

Re: Need RegEx expression for Strong passwords

Post by alicho »

Thanks for your response. However, i am still not able to fullfil the upper/lower (alternate) case requirement. Here is the complete requirement (notice the last clause and example)



- All passwords must be 8 or more characters in length
- AND must include at least one numeric (0-9) character
- AND one punctuation mark OR alternate case (upper/lower) character (for e.g. Bluesky1 or bluEsky1 or green&1&).



Greatly appreciate your help.sorry if i was not clear earlier about the last clause.

Also, the RegEx expression will be used in .NET. Do i need to change your 'raw' regEx expression to get that working in .NET
User avatar
ridgerunner
Forum Contributor
Posts: 214
Joined: Sun Jul 05, 2009 10:39 pm
Location: SLC, UT

Re: Need RegEx expression for Strong passwords

Post by ridgerunner »

Ok. But your definition is still not precise enough.
Can it contain whitespace?
Can it contain NO letters? (i.e. just punctuation and numbers?)
Precisely what defines "punctuation"?
Is it purely ASCII or can it contain unicode chars?

Assuming ASCII, no whitespace and no required letters, this one should do the trick. Shown first, commented in PHP syntax:

Code: Select all

if (preg_match(
    '/# Password == NUMBER && (PUNCTUATION || (UPPER && LOWER)).
    ^                    # Anchor to beginning of string.
    (?=\S*?[0-9])        # At least one number.
    (?:                  # And one of the two alternatives: Either..
      (?=\S*?[~!@#$%^&*()_+-={}[\]|:;<>,.?\/\'"`]) # One punctuation,
    | (?:                # or... (group upper && lower conditions...)
        (?=\S*?[A-Z])    # one uppercase letter and
        (?=\S*?[a-z])    # one uppercase letter.
      )                  # End upper && lower group.
    )                    # End 2 alternatives group.
    \S{8,}               # Match 8 or more non-space chars.
    $                    # Anchor to end of string.
    /x', 
    $contents)) {
    # Successful match
} else {
    # Match attempt failed
}
Here it is in raw format:

Code: Select all

^(?=\S*?[0-9])(?:(?=\S*?[~!@#$%^&*()_+-={}[\]|:;<>,.?/'"`])|(?:(?=\S*?[A-Z])(?=\S*?[a-z])))\S{8,}$
Post Reply