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.
- Strings deep:
reprto see hidden spaces,strip, indexing and slicing,find,in,count, why strings never change, format specs for tidy columns, and escapes and raw strings. - Split and join:
spliton a separator, on whitespace and with a limit, unpacking fields, the whole log read line by line,join, andpartitionforkey=valuetelemetry. - re basics:
re.searchand the Match object,\d,\w,\sand+, anchors withre.M,{m,n}and\b,re.splitandre.sub, and compiled patterns withre.I. - Groups:
group()andgroups(), a T-minus stamp turned into seconds, named groups,findallwith groups and(?:...), back-references inre.sub, andfinditerwith positions. - 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. - 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.
- 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:
reis 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 asPatternwhere CPython saysre.Pattern.- MicroPython on a microcontroller (a Pico, an ESP32) has a much smaller
built-in
re, with no{m,n}, no\band no named groups. The course says where a pattern would need changing to run there. strhas noljust,rjust,zfill,title,capitalize,isalnum,removeprefixortranslate. 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.59999999999999for 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.