Homework for Week 16
Regular expressions
Quiz
Go to Moodle and solve the quiz about the regular expressions.
Example
Example 1. Room codes in Liivi 2 read from the file
The file schedule.txt contains data about lectures and practice sessions (course name, place, and time) at the University of Tartu. The following program reads the data from the file and extracts the rooms' codes in the Institute of Computer Science building Liivi 2, using regular expressions. The program also prints the codes on the screen.
import re fname = input("Please enter filename: ") try: ffile = open(fname) except: print('File cannot be opened:', fname) else: for line in ffile: x = re.findall('Liivi 2 - ([0-9]+)', line) if len(x) > 0: for el in x: print(el)
Exercises
1. Username
Some computer networks assign a home directory to each network user. The users can create their home pages, which are located on the server. For example, the address of the home page at the server of the University of Tartu has the form http://www.ut.ee/~username/.
The file addresses.txt contains web addresses, each address on a separate line. Besides that, the file may contain other explanatory text. Write a program that reads the file, finds the lines containing the web addresses with usernames of the University of Tartu, and prints the usernames to the screen. The program must use regular expressions to search for usernames in web addresses. Other information, including usernames at other web addresses, should be skipped by the program.
For example, if the contents of the file addresses.txt are
http://www.ut.ee/~koit/KT/index_eng.html The lecture lasts 90 minutes. http://users.design.ucla.edu/~reas/ https://www.ut.ee/et/oppimine http://www.ut.ee/~vilo/ http://www.ut.ee/~kiho/ The notation "x ~ y" means that x and y are of the same order.
then the output of the program should be
Usernames are: koit vilo kiho
2. Strong password
Write a function is_strong that takes a password (a string) as its argument and uses regular expressions to check whether the password is strong. A strong password consists of at least eight characters, contains letters in both uppercase and lowercase, and at least one digit.
>>> is_strong("k4A8cd82B") True >>> is_strong("Bdy5Cez") False >>> is_strong("password") False
Hint. You may test the password against multiple regular expressions.
Submit your solutions
Go to Moodle and upload your solutions under homework for Session 16.