Strong password regular expression that matches any special char

^.*(?=.{7,})(?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[^a-zA-Z0-9]).*$

(Not C# code)

def validate (value):
    return (value.Length >= 7 &&
            value.IndexOfAny(['0', ..., '9']) >= 0 &&
            value.IndexOfAny(['A', ..., 'Z']) >= 0 &&
            value.IndexOfAny(['@', ..., ')']));

Yes I know this is not what the question required, but I believe it's much clearer, have higher performance and easier to maintain than any RegExp solution.


I believe that :-

\w

Matches any word character.

The inverse is :-

\W

Which is what you want.

Edit

^.*(?=.{7,})(?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[\W_]).*$

Test your regular expressions at :-

http://www.nregex.com/nregex/default.aspx

Tags:

C#

Regex