Stage 1 · Code
Standard Library Toolkit
Teach Text to Follow a Pattern
Regular expressions are not magic spells. They are pattern blueprints. In Go, the `regexp` package gives you matching, extraction, and replacement tools with a deliberately safe regex engine.
Patterns as Blueprints
Imagine you are sorting envelopes on a desk. You do not read each one like a novel. You glance for a shape: five digits means a postal code, three capital letters then a dash might be a team code, and a timestamp has its own recognizable arrangement. A regular expression is that shape written down.
Regex is powerful because it lets you describe families of strings instead of one exact string. But it is only helpful when you stay honest about the problem. If a plain string check works, use that. Reach for regex when the text really does have a pattern worth describing.
Go uses the RE2 regex engine. That means no backtracking-based tricks like lookahead or lookbehind, but it also means protection from catastrophic backtracking. In other words, Go gives up some cleverness so your program is less likely to freeze on a malicious or unlucky input.
Compiling and Matching
A pattern starts life as text, but the regexp package turns it into a compiled object that can match efficiently. That is why you usually compile once, then reuse the result many times.
| Function | Use it when | Behavior on bad pattern |
|---|---|---|
regexp.MustCompile | The pattern is a constant you control | Panics immediately |
regexp.Compile | The pattern comes from config, input, or other uncertain sources | Returns an error |
Panics are not something to scatter carelessly around a program. But they are acceptable when a constant pattern being invalid would mean the program itself was written incorrectly. In that case, failing loudly at startup is often better than limping onward.
Capturing and Extracting
Matching answers yes or no. Extraction answers 'which pieces did you find?'. Parentheses create capture groups, so one pattern can both validate a line and pull out useful parts from it.
Replacing Text
Regex is not only for validation and extraction. It can also rewrite text according to a pattern. That is useful for masking secrets, normalizing data, or cleaning up machine-generated strings.
One more safety note: because Go uses RE2, some regex patterns you may see in other languages simply will not compile here. Lookahead and lookbehind are the most common examples. That is not a missing feature by accident. It is part of the package's design contract.
Quiz
When is `regexp.MustCompile` a good choice?
Practice
Write a function `ExtractServiceAndLevel(line string) (service string, level string, ok bool)` that reads log lines shaped like `level=ERROR service=payments` anywhere inside the string. If both pieces are present, return them and `true`; otherwise return empty strings and `false`.
Summary
- A regex describes a family of text shapes, not just one exact string.
- Compile once and reuse when possible.
MustCompileis for fixed patterns you own;Compileis for uncertain patterns that may fail.- Capture groups let one regex both validate and extract structured pieces.
- Go's RE2-based engine gives up some advanced features in exchange for predictable performance and safety.
Mark this lesson complete to store local progress and unlock a cleaner resume path the next time you visit.