SoFunction
Updated on 2024-11-17

Rookie using python to implement regularity checking password legitimacy

Customer system upgrades, user passwords are required to meet certain rules, that is: contains upper and lower case letters, numbers, symbols, the length is not less than 8, so the first python wrote a simple test program:

Before writing the solution, list

Special characters in python regular expressions:

^ Indicates that the matching character must be at the top of the list.

$ means the matching character must be at the end

* matches * the first character 0 or n times

+ matches the characters before + 1 or n times

? Match ? 0 or 1 times for the preceding character

. (decimal point) matches all characters except newlines
(x) match x and record the matching value

x|y Match x or y

{n} Here n is a positive integer. Match the first n characters

{n, } Here n is a positive integer. Match at least n preceding characters

{n, m} Here n and m are positive integers. Match at least n and at most m preceding characters
[xyz] list of characters, matches any character in the table, can be concatenated - indicates a range of characters, e.g. [a-z] for all lowercase characters

[b] Match a space

b matches a word divider, such as a space

B matches a word's non-separating line

re module matching rules (third argument of the function):

Ignore case in the text

Handling character set localization

Whether to support multi-line matching

Matches some special tokens, such as the use of . to match characters such as \n

Ignore spaces and newlines in regular expressions.

Using Unicode

#encoding=utf-8

#-------------------------------------------------------------------------------
# Name: Module 1
# Purpose:
#
# Author:   Administrator
#
# Created:   10-06-2014
# Copyright:  (c) Administrator 2014
# Licence:   <your licence>
#-------------------------------------------------------------------------------
import re

def checklen(pwd):
  return len(pwd)>=8

def checkContainUpper(pwd):
  pattern = ('[A-Z]+')
  match = (pwd)

  if match:
    return True
  else:
    return False

def checkContainNum(pwd):
  pattern = ('[0-9]+')
  match = (pwd)
  if match:
    return True
  else:
    return False

def checkContainLower(pwd):
  pattern = ('[a-z]+')
  match = (pwd)

  if match:
    return True
  else:
    return False

def checkSymbol(pwd):
  pattern = ('([^a-z0-9A-Z])+')
  match = (pwd)

  if match:
    return True
  else:
    return False

def checkPassword(pwd):

  # Determine if the password length is legal
  lenOK=checklen(pwd)

  # Determine if it contains uppercase letters
  upperOK=checkContainUpper(pwd)

  # Determine if it contains lowercase letters
  lowerOK=checkContainLower(pwd)

  # Determine if it contains numbers
  numOK=checkContainNum(pwd)

  # Determine if it contains symbols
  symbolOK=checkSymbol(pwd)

  print(lenOK)
  print(upperOK)
  print(lowerOK)
  print(numOK)
  print(symbolOK)
  return (lenOK and upperOK and lowerOK and numOK and symbolOK)


def main():
  if checkPassword('Helloworld#123'):
    print('Test passed')
  else:
    print('Test failed')


if __name__ == '__main__':
  main()

Usually not much with the regular, do not know how to write a regular to meet the requirements, with a relatively stupid way, who knows a regular test please enlighten!

Let's take a look at a slightly more complex one. Detecting email name and password authentication

Code:

# coding=gbk 
 
import re 
 
def ProcessMail(inputMail): 
  isMatch = bool((r"^[a-zA-Z](([a-zA-Z0-9]*\.[a-zA-Z0-9]*)|[a-zA-Z0-9]*)[a-zA-Z]@([a-z0-9A-Z]+\.)+[a-zA-Z]{2,}$", inputMail,)); 
  if isMatch: 
    print ("Mailbox registration successful."); 
  else: 
    print ("Mailbox registration failed."); 
  return isMatch; 
 
def ProcessPassword(inputPassword): 
  #Handling Regular Expressions
  isMatch = bool((r"[a-zA-Z0-9]{8}",inputPassword,flags=0)); 
 
  # Use the three digits of type to indicate whether the number type[0], the lowercase letter type[1], and the uppercase letter type[2] all have the
  if isMatch: 
    type = [False,False,False] 
    for i in range(0,8): 
      temp = inputPassword[i] 
      if ord(temp) >= ord('0') and ord(temp) <= ord('9'): 
        type[0] = True; 
      elif ord(temp) >= ord('a') and ord(temp) <= ord('z'): 
        type[1] = True; 
      elif ord(temp) >= ord('A') and ord(temp) <= ord('Z'): 
        type[2] = True; 
    for i in type: 
      if i is False: 
        isMatch = False; 
        break; 
   
  # Handle whether duplicate characters appear
  if isMatch: 
    for i in range(0,7):  
      temp = inputPassword[i]; 
      for j in range(i + 1,8): 
        if inputPassword[j] == temp: 
          isMatch = False; 
          break; 
   
  if isMatch: 
    print ("Password registration successful."); 
  else: 
    print ("Password registration failed."); 
  return isMatch; 
   
if __name__ == '__main__': 
  mailState = False; 
  while mailState is False: 
    inputMail = input("Enter mailbox: "); 
    mailState = ProcessMail(inputMail); 
    print ("\n"); 
#   
  passwordState = False; 
  while passwordState is False: 
    inputPassword = input("Enter password: "); 
    passwordState = ProcessPassword(inputPassword); 
    print ("\n"); 

Output:

Enter your email address: a.a9@ 
Email registration failure。 

Enter your email address: @ 
Email registration failure。 

Enter your email address: @ 
Email registration failure。 

Enter your email address: a9999@ 
Email registration failure。 

Enter your email address: @..com 
Email registration failure。 

Enter your email address: @a..com 
Email registration failure。 

Enter your email address: @ 
Email registration failure。 

Enter your email address: @ 
Email registration failure。 
 
Enter your email address: @ 
Email Registration Successful。 

Tests for passwords also fulfill the requirements, to name a few