简体   繁体   中英

Strong password regular expression that matches any special char

I need the following check for strong password validation:

  • At least 7 chars
  • At least 1 uppercase char (AZ)
  • At least 1 number (0-9)
  • At least one special char

I found and tweaked a RegEx and it's like this (sorry, I lost the reference...):

^.*(?=.{7,})(?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[@'#.$;%^&+=!""()*,-/:<>?]).*$

It's working in C# except for the fact that I need to match any special char, and I really mean ANY . In other words, I need that the "special char" be anything but numbers and lower/uppercase letters.

Edit:

For the sake of clarity, let's consider that accents are special chars, so é , ñ and the like should be considered special chars in the context of this question.

^.*(?=.{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

看看这里: Unicode正则表达式并选择一个Unicode类,如\\p{Symbol}\\p{Punctuation}

尝试这个:

^(?=.{7,})(?=.*?\d)(?=.*?[a-z])(?=.*?[A-Z])(?=.*?[@'#.$;%^&+=!"()*,-/:<>?])

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM