Text and regular expressions lesson plan
Strings, split and join, then regular expressions from search to named groups, on a rocket launch log and a ship's GPS feed, ending in a log analyzer on a Raspberry Pi.
Objectives and materials
By the end, students can
- What a string really holds, and how to cut a line into fields.
- Describe text instead of spelling it out, and pull out the pieces.
- A launch log and a ship's GPS, turned from text into data.
- A log analyzer that answers commands and lights an alarm.
Materials for the whole unit
- Half breadboard
- LED x 2
- Raspberry Pi 4 Model B
- Resistor x 2
Every part is already on the simulator’s bench, so there is nothing to buy or hand out. The counts are the most any one lesson uses, if you build it on real breadboards too.
Text, cut and glued
What a string really holds, and how to cut a line into fields.
| Lesson | Time | What students do |
|---|---|---|
| Strings deepOne line of a rocket launch log, taken apart: repr and strip, indexing and slicing, find, in and count, why strings never change, format specs for tidy tables, and escape characters and raw strings. | 40 min |
|
| Split and joinsplit() on a separator, on whitespace and with a limit, unpacking fields into names, reading a whole log file line by line, join() to glue text back together, and partition() for key=value telemetry. | 40 min |
|
Materials
- Half breadboard
- LED x 2
- Raspberry Pi 4 Model B
- Resistor x 2
Check for understanding
Each student finishes "Split and join" on their own, without opening the hints. The simulator checks 6 of its 6 steps as they go.
Then ask each one to explain, in a sentence or two, the last thing it asked of them: "partition for key=value".
Regular expressions
Describe text instead of spelling it out, and pull out the pieces.
| Lesson | Time | What students do |
|---|---|---|
| re basicsRegular expressions with re: search and the Match object, character classes and repeats, anchors and MULTILINE, counted repeats and word boundaries, re.split and re.sub, and compiled patterns with flags. | 40 min |
|
| GroupsBrackets that capture: group() and groups(), turning a T-minus stamp into seconds, named groups and groupdict, findall with groups and non-capturing groups, back-references in re.sub, and finditer with positions. | 40 min |
|
Materials
- Half breadboard
- LED x 2
- Raspberry Pi 4 Model B
- Resistor x 2
Check for understanding
Each student finishes "Groups" on their own, without opening the hints. The simulator checks 6 of its 6 steps as they go.
Then ask each one to explain, in a sentence or two, the last thing it asked of them: "Every match, with positions".
Parsing real feeds
A launch log and a ship's GPS, turned from text into data.
| Lesson | Time | What students do |
|---|---|---|
| Parsing logsA parser for the whole launch log: one regex per line into (time, system, message) records, telling comments from garbage, counting by system, formatting stamps back, timing events by pattern, and pulling a time window. | 40 min |
|
| Parsing NMEA linesA ship's GPS feed in NMEA 0183: the XOR checksum that catches corrupted sentences, GGA fields by position, degrees and minutes to decimal degrees, a regex that validates and sorts sentence types, and speed and course from RMC. | 40 min |
|
Materials
- Half breadboard
- LED x 2
- Raspberry Pi 4 Model B
- Resistor x 2
Check for understanding
Each student finishes "Parsing NMEA lines" on their own, without opening the hints. The simulator checks 6 of its 6 steps as they go.
Then ask each one to explain, in a sentence or two, the last thing it asked of them: "Speed and course".
Project
A log analyzer that answers commands and lights an alarm.
| Lesson | Time | What students do |
|---|---|---|
| Project: a log analyzerEverything in one tool: load and parse the launch log, then answer typed commands (grep with any regex, a time window, ranked stats), survive a broken pattern, and light a red alarm LED when the log holds warnings. | 50 min |
|
Materials
- Half breadboard
- LED x 2
- Raspberry Pi 4 Model B
- Resistor x 2
Check for understanding
Each student finishes "Project: a log analyzer" on their own, without opening the hints. The simulator checks 7 of its 7 steps as they go.
Then ask each one to explain, in a sentence or two, the last thing it asked of them: "Make it yours".
Teach this unit as a class
Start a class from this course and its 7 lessons are assigned in one go, in order. Students join with a six-character code or printed login cards, and each one’s work comes back to you.
The class tools are free for 30 days for one class of up to 20 students, with no card. The lessons themselves are free either way.