In this Python tutorial, we will take a look into finding a pattern in text using regular expressions to validate a password with a set of requirements that must be met for password validation. A regular expression is commonly used to search patterns in a string or text in any programming language.
Finding Pattern in Text (re.search())
In order to use search() function, you need to import re first and then execute the code. The search() function takes the “pattern” and “text” to scan from our main string and returns a match object when the pattern is found or else not match if the pattern is not found.
Primary conditions for password validation :
- Minimum of 8 characters
- Maximum of 15 characters
- Must have lowercase alphabetical characters: a-z
- Must have one alphabetical character should be of Upper Case: A-Z
- Must have one number between 0-9
- Must have one special character from !@#$%^&*()
Python Regular Expression (re) Module
The Python re module provides regular expression operations for matching both patterns and strings to be searched can be Unicode strings as well as 8-bit strings. A regular expression matches a specified set of strings that can be given with a customer variable (Which we will do with a symbols in this tutorial) to validate again the string.
char.islower()
The islower() function in Python is a string method that can be used to check if a given string has lower case letters. If islower() does not validate that there are lower case characters it will return a value of False. If it is lower then the function will return True.
char.isupper()
The isupper() function in Python is a string method that can be used to check if a given string has upper case letters. If isupper() does not validate that there are upper case characters it will return a value of False. If it is lower then the function will return True.
char.isdigit()
The isdigit() function in Python is a method that can be used to cross check a string and validated if there any digits within that string. If isdigit() validated there is a digit in the string, it will return a value of True otherwise, it will return a value of False.
Regular Expression Password Validation Example
Below is the full code used to validate a password to meet the requirements given. In addition, there are two examples with invalid password input and valid password input.
Input:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 | import re def passwordValidation(password): invalid = True symbols = ['!', '@', '#', '$', '%', '^', '&', '*', '(', ')'] if len(password) < 8: print('Password does meet the minimum length requirements of 8 characters.') invalid = False if len(password) > 15: print('Password should not exceed 15 characters.') invalid = False if not any(char.islower() for char in password): print('Password should have at least one lowercase letter.') invalid = False if not any(char.isupper() for char in password): print('Password should have at least one uppercase letter.') invalid = False if not any(char.isdigit() for char in password): print('Password should have at least one number.') invalid = False if not any(char in symbols for char in password): print('Password should have at least one of the symbols: !@#$%^&*() ') invalid = False if invalid: return invalid def main(): users = dict() username = input("Please create a new username: ") print("""The password must meet the below requirements: Minimum of 8 characters Maximum of 15 characters Must have lowercase alphabetical characters: a-z Must have one alphabetical character should be of Upper Case: A-Z Must have one number between 0-9 Must have one special character from !@#$%^&*() """) password = input("Please create a password: ") if (passwordValidation(password)): print("Password is valid") users[username] = password print(users) else: print("Invalid Password !!") if __name__ == '__main__': main() |
Invalid Password Example
User Input with Invalid Password:
1 2 3 4 5 6 7 8 9 10 | Please create a new username: <span style="color: #000000;">PythonRegEx</span> The password must meet the below requirements: Minimum of 8 characters Maximum of 15 characters Must have lowercase alphabetical characters: a-z Must have one alphabetical character should be of Upper Case: A-Z Must have one number between 0-9 Must have one special character from !@#$%^&*() Please create a password: <span style="color: #000000;">abc123</span> |
Output with Invalid Password:
1 2 3 4 | Password does meet the minimum length requirements of 8 characters. Password should have at least one uppercase letter. Password should have at least one of the symbols: !@#$%^&*() Invalid Password !! |
Valid Password Example
User Input with Valid Password:
1 2 3 4 5 6 7 8 9 10 | Please create a new username: PythonRegEx The password must meet the below requirements: Minimum of 8 characters Maximum of 15 characters Must have lowercase alphabetical characters: a-z Must have one alphabetical character should be of Upper Case: A-Z Must have one number between 0-9 Must have one special character from !@#$%^&*() Please create a password: r4Bm@xmq |
Output with Valid Password:
1 2 | Password is valid {'PythonRegEx': 'r4Bm@xmq'} |
I hope this tutorial was helpful in learning about regular expressions and searching within a string to match a pattern.