How to judge the strength of a password by using regular expressions
How to determine the strength of a password by using regular expressions? Many novices are not very clear about this. In order to help you solve this problem, the following editor will explain it in detail. People with this need can come and learn. I hope you can gain something.
Def password_level (password): weak = re.compile (r'^ ((\ d +) | ([A-Za-z] +) | (\ W+)) $') level_weak = weak.match (password) level_middle = re.match (r'([0-9] +) (\ W+ |\ _ + | [A-Za-z] +)) + | ([A-Za-z] + (\ W+ |\ _ + |\ d+)) + | ((\ W+ |\ _ +) + (\ d+ |\ w+)) + Password) level_strong = re.match (r'(\ w + |\ W+) +', password) if level_weak: print 'password level is weak',level_weak.group () else: if (level_middle and len (level_middle.group ()) = = len (password)): print' password level is middle',level_middle.group () else: if level_strong and len (level_strong.group ()) = len (password): print 'password level is strong',level_strong.group ()
Explain
Weak passwords: all numbers, symbols, letters
Medium password: numbers plus symbols, numbers plus letters, letters plus symbols
Strong password: three mixtures.
I am not case-sensitive, so I hope those who are interested can write it themselves. the problem occurs on\ w because\ w is equivalent to [A-Za-z0-9], so the string containing the descending line cannot be matched by\ W in the early stage.
Let's take a look at the medium password, number plus symbol or letter or _ is a group, letter plus symbol or underscore or symbol is a group, symbol or underscore plus letter or number is a group. I always think that the code in this does not seem to be right, but there is nothing wrong through the test, so let's use this version 0.0.1 first.
Test code
If _ _ name__ = ='_ main__': passwords = ('11a), LLZ, 1aL, 1a, 1a, 1a, 1a.