Lessons and the learning game

Text and regular expressions

Parse a rocket's launch log and a ship's GPS feed with strings, split and join, and regular expressions, then build a log analyzer on a Raspberry Pi.

The course

Text and regular expressions is an intermediate Coding course in seven lessons on a Raspberry Pi 4 with two LEDs (red on GPIO17, yellow on GPIO27). Two real feeds run through it: the launch log of Mokxi-1 (a tab called launch_log.txt, with a comment, a blank line and one line garbled by a telemetry dropout) and six minutes of NMEA 0183 from a research ship leaving Monterey Bay (nmea.txt, with real checksums, one corrupted sentence and one cut short). Each step is checked against what the Pi prints.

  1. Strings deep: repr to see hidden spaces, strip, indexing and slicing, find, in, count, why strings never change, format specs for tidy columns, and escapes and raw strings.
  2. Split and join: split on a separator, on whitespace and with a limit, unpacking fields, the whole log read line by line, join, and partition for key=value telemetry.
  3. re basics: re.search and the Match object, \d, \w, \s and +, anchors with re.M, {m,n} and \b, re.split and re.sub, and compiled patterns with re.I.
  4. Groups: group() and groups(), a T-minus stamp turned into seconds, named groups, findall with groups and (?:...), back-references in re.sub, and finditer with positions.
  5. Parsing logs: one regex per line into (seconds, system, message) records, comments told apart from damage, counts by system, warnings with stamps, the first stage burn (154 s) and a time window.
  6. Parsing NMEA lines: the XOR checksum that catches the corrupted fix (and the swap it cannot catch), GGA fields by position, degrees and minutes to decimal degrees, a validating regex, and speed and course from RMC.
  7. Project: a log analyzer: the finale.

The finale

The analyzer loads and parses the launch log, prints a summary (17 events, 2 warnings, 1 unreadable) and then answers commands typed in the Serial Monitor: grep with any regular expression, between T+01:00 T+03:00 for a time window, stats for every system ranked, and quit. A broken pattern like grep ( is reported with re's own message instead of crashing the tool. The red LED on GPIO17 is the alarm: lit whenever the log holds a warning. It is switched on before the prompt, because a program waiting at input() runs nothing else.

What the Pi's engine does differently

The Pi runs MicroPython 1.29 with a CPython-style standard library:

  • re is the engine's own, written in Python with CPython's syntax: named groups, {m,n}, \b, lookarounds and the flags all work, so every pattern in the course runs on CPython unchanged. It is slower, and a compiled pattern's type prints as Pattern where CPython says re.Pattern.
  • MicroPython on a microcontroller (a Pico, an ESP32) has a much smaller built-in re, with no {m,n}, no \b and no named groups. The course says where a pattern would need changing to run there.
  • str has no ljust, rjust, zfill, title, capitalize, isalnum, removeprefix or translate. Format specs ({name:<8}, {n:03d}) do the same jobs and work on every Python.
  • str() of a float can print more digits than CPython does (98.59999999999999 for 98.6), and rounding an exact halfway value can land on a different last digit. Printing with a format spec like {n:.1f} gives the same text everywhere.
  • A dict does not keep the order keys went in, so the lessons read keys by name or sort them.