Home > Web Front-end > JS Tutorial > Share an example of using regular expressions to determine the strength of a password

Share an example of using regular expressions to determine the strength of a password

零下一度
Release: 2017-06-17 10:54:33
Original
1473 people have browsed it

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()
Copy after login

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 = (&#39;11&#39;,&#39;aa&#39;,&#39;LL&#39;,&#39;1a&#39;,&#39;1_&#39;,&#39;a_&#39;,&#39;a1&#39;,&#39;_1&#39;,&#39;*a&#39;,&#39;1a_&#39;,&#39;1a<&#39;)
 for pw in passwords:
  password_level(pw)
&#39;&#39;&#39;----------------------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<
&#39;&#39;&#39;
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template