In: Computer Science
Pattern matching using python3 re module
Write a regular expression that will find out all the words that ends with 4 consecutive vowels at the end.
Example: import re
f=open("/usr/share/dict/american-english",'r')
pattern='a[a-z]*d$'
words=f.readlines()
matchlist=[word for word in words if re.match(pattern,word)]
===============================
Generate random string follow a specific pattern
You will take a username and ask user for the password. User has to enter a strong password in order to create his or her account. The criteria for strong password is, a) Starts with a letter a to z or A to Z b) Cannot end with a digit c) Total character should be 10 d) The character can be letter a to z or A to Z or digit 0 to 9 or _
You will show a message that account is successfully created if user enters a strong password. If a user does not enter a strong password, you will reject the user entered password and generate a strong password as follows: a) Starts with the first character of the user’s user name b) Ends with the last character of the user’s user name c) Total character should be 10 d) The character can be letter a to z or A to Z or digit 0 to 9 or _
Sample output: Account is successfully created
Another sample output: Week Password
Generating a Strong Password
The auto generated password: j57m9XoUGa
Question #1
The program is given below. The comments are provided for the better understanding of the logic.
import re
#Provide the filename here. If it is in a separate directory, the full path has to be entered here.
#Assumed that the file has only one word per line.
#If this is not the case, logic has to be altered.
inputFile = "words.txt"
#Open the file in read only mode
fileHandler = open(inputFile,"r")
#Read the 1st line
line = fileHandler.readline()
#This loop will iterate as long as there are lines in the file.
while(line):
#Trim the extra line breaks at the end.
line = line.strip()
#Check if the line matches the pattern "[aeiou]{4}$". This is for matching any letter ending with 4 vowels.
match = re.search("[aeiou]{4}$", line.lower())
#Print the word if there is a match
if(not (match is None)):
print(line)
#Read the next line for the loop to continue.
line = fileHandler.readline()
#Close the file.
fileHandler.close()
The screenshots of the code and output are provided below.
Question #2
The program is given below. The comments are provided for the better understanding of the logic.
import re
import random
import string
#Prompt for the username and password.
#And store it in variables
username = input("Enter your username: ")
password = input("Enter your password: ")
#Check if the password entered matches all the rules given.
#In the below regex pattern
#[a-zA-Z] matches a single alphabet in lower or upper case
#[a-zA-Z0-9_]{8} matches alphabets or digits or an underscore. It has to be exactly 8 characters.
#[a-zA-z_] matches a single character. It can be alphabet or underscore. The rule given is that the password cannot end with a digit. Hence coded like this.
match = re.search("^[a-zA-Z][a-zA-Z0-9_]{8}[a-zA-z_]$", password)
if(match is None):
#If there is no match, a week password is entered.
#Generate a new password
print("This is a Week Password")
print("Generating a Strong Password")
#This cariable stores lowercase and uppercase letters, digits and underscore.
characterSequence = string.ascii_lowercase + string.ascii_uppercase + "0123456789_"
generatedPassword = ""
#This loop iterates from 0 to 7.
for i in range(8):
#The random.choice function randomly select a character from the characterSequence variable
generatedPassword = generatedPassword + random.choice(characterSequence)
#at the end of the above loop, the variable generatedPassword will have a 8 length random set of characters.
#prefix with 1st letter of the username and suffix with the last letter of the username.
#Thus forming a 10 character password.
generatedPassword = username[0] + generatedPassword + username[len(username)-1]
#Display the password to the user.
print("The auto generated password: ", end = "")
print(generatedPassword)
else:
#If the user entered a proper password, print a success message.
print("The account is successfully created.")