Password validation in python with regex without duplicate chars

2024/7/7 7:31:36

The parameter is a string. Check whether it forms a secure password.A password is safe if

  • there is at least a lowercase letter in it, and
  • there is at least one capital letter in it, and
  • there is at least one digit in it, and
  • there is at least one special character (+, -, *, /,. or @) in it, and
  • it counts at least 8 characters, and
  • it has a maximum of 20 characters, and
  • there are no three equal consecutive characters in it (eg 'a7XXX @ fda' is invalid because of the XXX), and
  • no group of three characters occurs multiple times (eg 'XYZ.1a.XYZ' is invalid because XYZ appears twice).

I tried:

regex = [r'^.{8,20}$', r'[a-z]+', r'[A-Z]+', r'\d+', r'[-+*/.@]', r'(.)(.)(.)(\1\2\3)']return all(re.search(pas, password) for pas in regex)
Answer

You can use lookahead assertions:

import rereg=re.compile(r"(?=.*[a-z]+.*)(?=.*[A-Z]+.*)(?=.*\d+.*)(?=.*[-\+\*\./@]+.*)(?!.*(.)\1\1.*)(?!.*(.{3}).*\2.*).{8,20}$")pwds=[ "aA3@5678", "aA3@567","abcdefghABCDEFGH+-*3","abcdefghABCDEFGH+-*3X","aA3@555678","aA3@@@5678", "a_A3Q_5678*A3Q", "a_A+Q_3.14/-5678*A+Q"]for pwd in pwds:m=reg.fullmatch(pwd)print("{}: {}".format(pwd,"good" if m else "bad"))
https://en.xdnf.cn/q/120256.html

Related Q&A

Processing non-english text

I have a python file that reads a file given by the user, processes it, and ask questions in flash card format. The program works fine with an english txt file but I encounter errors when trying to pro…

Downloading all zip files from url

I need to download all the zip files from the url: https://www.ercot.com

sql to query set

I have 2 tables:puzz_meeting_candidats :- id, canceled, candidat_id, meeting_id puzz_meeting :- id, ClientI have a query follow: SELECT U1.`candidat_id` AS Col1 FROM `puzz_meeting_candidats` U1 INN…

Google App Engine, best practice to schedule code execution [closed]

Closed. This question is opinion-based. It is not currently accepting answers.Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.Clo…

delete rows by date and add file name column for multiple csv

I have multiple "," delimited csv files with recorded water pipe pressure sensor data, already sorted by date older-newer. For all original files, the first column always contains dates forma…

X = Y = Lists vs Numbers [duplicate]

This question already has answers here:Immutable vs Mutable types(20 answers)How do I clone a list so that it doesnt change unexpectedly after assignment?(24 answers)Closed 4 years ago.In python : I h…

Python data text file grades program

Looking for help with my program. There is a text file with 5 first and last names and a number grade corresponding to each person. The task is to create a user name and change the number grade to a le…

how to fill NA with mean only for 2 or less consequective values of NA

I am new to python. please help me how I should proceed. The following dataframe contains large blocks of NaNs. # Fill the NAs with mean only for 2 or less consecutive values of NAs. # Refer to the d…

Build a new dictionary from the keys of one dictionary and the values of another dictionary

I have two dictionaries:dict_1 = ({a:1, b:2,c:3}) dict_2 = ({x:4,y:5,z:6})I want to take the keys from dict_1 and values from dict_2 and make a new dict_3dict_3 = ({a:4,b:5,c:6})

Python 2.7 - clean syntax for lvalue modification

It is very common to have struct-like types that are not expected to be modified by distant copyholders.A string is a basic example, but thats an easy case because its excusably immutable -- Python is …