Homework for Week 16
After this week you can
- Write simple regular expressions for string processing
- Use functions from the regular expressions module to search and extract information
Regular expressions
This is a short introduction to searching information in a text. Regular expressions are a language that allows us to specify different searching tasks in a concise way. This language relies on special characters, each with a specific meaning. Writing and debugging a regular expression can be a programming task of its own.
The following videos explain the concept in more detail:
Quiz
Go to Moodle and solve the quiz about the regular expressions.
Example
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)
The function findall returns the list of all matches in the order they were found in the string. After that, we process the list if it is non-empty.
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. The program should skip other information, including usernames at other web addresses.
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.