Teacher's Guide
Chapter 9 / Level 2: Regex Module
Teaching Objective:
- Student learns how regular expressions work and how to use them to find string patterns
- Learns the syntax and usage of the various Regex functions and their implementations
- Understands the purpose of regular expressions and use cases in practical exercises
Guide
In this exercise we’ll be working with the re module which stands for Regular Expressions or Regex for short. The module is imported by writing import re , and the functions it gives us access to are designed to manipulate or identify elements within strings. For our purposes the functions and elements we’ll be using are as follows:
re.findall(): Return a list of all occurrences of a string. Takes two (2) arguments, the first is what characters you’re looking for in a string, the second is the string to search in.re.sub(): Replaces indicated occurrence with a certain sting. Takes three (3) arguments, the first is what characters you would like to replace, the second is what you would like to replace it with and the third is the string you would like to search in.re.search(): Find the location of something in a string, returns an object. Takes two (2) arguments, the characters you’re searching for and the string you’re searching in. You can use other functions with the object it returns such asspan()which returns a vector of the searched item’s start and end positions in the text.re.split(): Splits string into a list at specified intervals. Takes two(2) arguments, the first where to split the string by and the second is the string you would like to split.re.match(): Checks if a string contains a certain value at the start of the string. This works like a simplified versionre.search()function but is more efficient and checks if the string you’re searching in has the query at the start.
The re module can also use special sequences, there are codes that you can use with the re functions that allow you to encompass a variety of string properties. There is a long list of special sequences but for this level we will be using the following:
\B: Checks if specified characters are in the string but not located at the start or end of a word.\D: Returns characters are not digits 0-9
If an r is present with a special sequence, it means that it’s checking the raw string.
The objective in this level is to verify and edit employee rosters and Zoning sectors for farmland. We start off by walking to the gold X mark and using read() to check the employee manifest, who’s data is stored in a constant named manifest. Create a list named tags and store the return value of re.findall() which will isolate how many "#" are in the manifest string. Create a variable named number and store the length of the tags list using len() to get the number of employees that are in the manifest. Use speak() to announce number.
tags = re.findall("#", manifest)
number =len(tags)
player.speak(number)
Walk to light X mark and use read() to check the new employee hires and make note of them. Move onto the dark X mark over the blue carpet and use read() to check the currently assigned employees, which are stored in a constant named assignments. Use the re,sub() function to replace elements that are incorrect on the assignments constant with the correct new assignments previously read. Use write() with the assignments constant to update it.
assignments = re.sub("Billy Hodgins","Carol Hopkins", assignments)
assignments = re.sub(-inser value-, -inser value-, assignments)
await player.write(assignments)
Walk to the dark X mark over red carpet and use read() to check out the new Zoning locations for the farmland, which are also stored in a constant named zones. Create a variable named index and store the value of re.search() with the"\B" special sequenceto find the location in the document where the zone 6210 is found in the document. Create another variable named vector and store the value of index.span() to generate the vector position of the zone in the document. Use the vector variable with the pre-written write() function.
index = re.search(r"6210\B", zones)
vector = index.span()
await player.write(
"The 6210 zone sector is located: [%s,%s]" % (vector))
Walk to the dark X mark over the green carpet, create a list named sectors and store the value of re.split() function with the "\D" special sequence to extract all individual elements from the zones. Use a list comprehension to remove any elements with a length of less than 3 using len(). Use write()with the sectors variable to write down all the elements.
sectors = re.split("\D", zones)
sectors = [xfor xin sectorsiflen(x) >3]
await player.write(sectors)
Walk to the light X mark and use read() and make note of priority sectors so you may verify if they’re present in the sectors list. Walk to the dark X mark over the purple X mark and populate the pre-written sector variables with the information you just read so we can check if they’re in the sectors list using the re.match() function.
sector_a = [xfor xin sectorsif re.match("3825", x)]
sector_b = [xfor xin sectorsif re.match("9876", x)]
sector_c = [xfor xin sectorsif re.match("8274", x)]
await player.write(
"Priority sectors-\nSector A: %s\nSector B: %s\nSector C: %s\n"
% (sector_a,sector_b,sector_c))
Use the the sector variables with the pre-written write() function to complete the level.
Map overview and path

Code Solution
import player
import re
player.turn_right()
player.move_forward(6)
player.turn_left()
await player.read()
tags = re.findall("#", manifest)
number = len(tags)
player.speak(number)
player.turn_right()
player.move_forward(4)
player.turn_right()
await player.read()
player.turn_left()
player.move_forward()
player.turn_right()
await player.read()
assignments = re.sub("Billy Hodgins", "Carol Hopkins", assignments)
assignments = re.sub("Jacob Warner", "Daniel Foster", assignments)
await player.write(assignments)
player.turn_left()
player.move_forward()
player.turn_right()
player.move_forward(3)
player.turn_right()
player.move_forward()
player.turn_left()
await player.read()
index = re.search(r"6210\B", zones)
vector = index.span()
await player.write(
"The 6210 zone sector is located: [%s,%s]" % (vector))
player.turn_right()
player.move_forward()
player.turn_left()
sectors = re.split("\D", zones)
sectors = [x for x in sectors if len(x) > 3]
await player.write(sectors)
player.turn_right()
player.move_forward(2)
player.turn_left()
await player.read()
player.turn_right()
player.move_forward()
player.turn_left()
sector_a = [x for x in sectors if re.match("3825", x)]
sector_b = [x for x in sectors if re.match("9876", x)]
sector_c = [x for x in sectors if re.match("8274", x)]
await player.write(
"Priority sectors-\nSector A: %s\nSector B: %s\nSector C: %s\n"
% (sector_a,sector_b,sector_c))