When we are going to make any user authentication more secure, user has to provide secure password while creating user account. When user provide strong password, then account should be more secure. In this python example we are showing Password validation with regex. To use regex in python we need to import "re" module to our python example.
This example we are validating the password with regex equation which will validate below conditions
Let's write python regex example to validate password
import re
# input
print("Please enter password should be \n1) One Capital Letter\n2) Special Character\n3) One Number \n4) Length Should be 8-18: ")
pswd = input()
reg = "^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[@$!%*#?&])[A-Za-z\d@$!#%*?&]{8,18}$"
# compiling regex
match_re = re.compile(reg)
# searching regex
res = re.search(match_re, pswd)
# validating conditions
if res:
print("Valid Password")
else:
print("Invalid Password")
|
Steps to execute program
step 1: We are taking the password text as input from user.
step 2: creating the regex compiler equation with given validation string
step 3: "re" module search() method we are passing the reg expression and user password input. This re.search() will be validate given string matches to the corresponding regex condition or not and return true/false response.
step 4: Based on this response we are printing the validate password result.
Output:
Example 1:
Please enter password should be |
Example 2:
Please enter password should be |
Related
Python String replacement with regex example
Article Contributed By :
|
|
|
|
6181 Views |