This article will share with you an example code that uses regular expression to determine the strength of a password. It is very good and has reference value. Friends who need it can refer to it
learn python's re template, I wrote an article and found that no one read it, so I summarized my experience. No one loves theory, but people's hearts are in practice. So since no one likes theory, just go to practice and refine the theory in practice. Without further ado, let’s just start with the code
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 it
Weak password: all numbers, symbols, letters
Medium password: Numbers plus symbols, numbers plus letters, letters plus symbols
Strong password: Mix of three.
I am not case sensitive, I hope those who are interested can write it themselves. The problem occurs with \w because \w is equivalent to [A-Za-z0-9_], so the string containing the underline cannot be matched through \W in the early stage
Let’s take a look Look at the medium password. Numbers plus symbols or letters or _ are a group. Letters plus symbols or underlines or symbols are a group. Symbols or underscores plus letters or numbers are a group. I always feel that the code in this seems wrong, but After passing the test and finding nothing wrong, just use this version 0.0.1 first
Test code
if name == 'main': passwords = ('11','aa','LL','1a','1_','a_','a1','_1','*a','1a_','1a<') for pw in passwords: password_level(pw) '''----------------------output------------------------ #password level is weak 11 #password level is weak aa #password level is weak LL #password level is middle 1a #password level is middle 1_ #password level is middle a_ #password level is middle a1 #password level is middle _1 #password level is middle *a #password level is strong 1a_ #password level is strong 1a< '''
The above is the detailed content of Share an example of using regular expressions to determine the strength of a password. For more information, please follow other related articles on the PHP Chinese website!