Computing Foundations › Systems Foundations: Storage, Observability, and Tooling › Day 38

Day 38: Regular Expressions

Day 38 of 365 — Regular Expressions

After this lesson you will be able to read and write regular expressions with confidence — describing the shape of text with literals, character classes, anchors, quantifiers, groups, and alternation — and use them from the command line to search, extract, and reformat real data.

Course
Computing Foundations
Category
Systems Foundations: Storage, Observability, and Tooling
Reading time
≈ 40 min
Practical time
≈ 30 min
Lesson duration
1h 10m
Last verified
2026-07-12

Hands-on lab for this lesson

Lab files on GitHub: https://github.com/ai-roadmap-365/ai-roadmap-365.github.io/tree/main/labs/sections/computing-foundations/day-038-regular-expressions

  1. Get the hands-on files. Clone the labs repository once (you can reuse this clone for every lesson). This works on macOS, Linux, and Windows (PowerShell or WSL):
    git clone https://github.com/ai-roadmap-365/ai-roadmap-365.github.io.git
    cd ai-roadmap-365.github.io
  2. Open this lesson's lab. Move into the directory for this specific day. Every lab lives at the same predictable path — section / subsection / week / day:
    cd labs/sections/computing-foundations/day-038-regular-expressions
  3. Read the lab guide. Open `README.md` in that directory. It lists the exact commands, what each does, the expected output, and how to check your work — read it before running anything.
  4. Run it and check your work. Follow the README's "How to run" section: run the example first to see the finished result, then complete the numbered exercises in `starter/`, then run the tests. The tests pass (exit 0) only when your work is correct.
    bash tests/run_tests.sh   # or the test command named in the lab README

You can also open the lab as a local page (works offline, shows the file tree and expected output).

Learning objectives

By the end of this lesson you will be able to:

Prerequisites

Why this matters


The idea in plain language

Diagram: the five kinds of component in a regular expression


Historical background


What it is — and what it is not

Diagram: text regex handles well compared with structures that need a parser

A regex is:

It is not:


Why it was created and what problems it solves


How it works

Building blocks

Anchors

Quantifiers

QuantifierMeaningExampleMatchesDoes not match
*zero or moreab*cac, abc, abbbcadc
+one or moreab+cabc, abbcac
?zero or oneab?cac, abcabbc
{n}exactly na{3}aaaaa
{n,}n or morea{2,}aa, aaaaa
{n,m}between n and ma{2,4}aa, aaa, aaaaa

Groups, alternation and backreferences

Diagram: how capture groups extract fields from a match

Diagram: the anatomy of a regular expression, with each part of a date pattern labelled

Greedy versus lazy

Diagram: how a greedy quantifier over-matches and how to fix it

How the engine scans

Diagram: a regex engine scanning a string left to right, finding a date match and handing back three captured groups


An everyday analogy

Diagram: how regex lookahead asserts without consuming characters

Describing a person to someone meeting them at a station.

The descriptionThe regex
”Wearing a red coat”A literal
”Red, blue or green”A character class
”First person off the train”An anchor
”Carrying one or more bags”A quantifier
”Note their name and their platform”Capture groups
”As many bags as they can hold”Greedy
”At least one bag, no more”Lazy

Examples in practice

Diagram: differences between regular expression flavours

Diagram: greedy quantifiers compared with lazy ones


Implications: security, privacy, performance, scalability, and cost

Diagram: how nested quantifiers cause catastrophic backtracking


Alternatives: free, open source, and commercial

ApproachTypeWhere it fits
RegexBuilt in everywhereLine-oriented, locally structured text
jqFree, open sourceJSON — where regex should never be used
An HTML or XML parserFree, open sourceNested markup, which regex genuinely cannot do
str.split / startswithBuilt inSimple cases; often clearer than a pattern
A proper parser or grammarFree, open sourceAnything with nesting or recursion
regex101 and similarFreeExplaining a pattern piece by piece as you build it

TermWhat it actually is
LiteralA character matching itself
Character classAny one of a set
AnchorA position, matching no characters
QuantifierHow many times the previous thing repeats
GroupA sub-pattern, usually capturing what it matched
GreedyTakes as much as possible; the default
BacktrackingUndoing a choice to try another

When to use it — and when not to

Diagram: an unreadable regex compared with a documented one


The AI connection

Diagram: extracting model output with regex compared with requesting JSON


Knowledge check

quiz:
  question: "The pattern `<.*>` applied to the text `<a><b>` matches the whole string rather than just `<a>`. Why?"
  options:
    - "Quantifiers are greedy by default, so `.*` consumes as much as possible while still allowing a final `>`"
    - "The dot matches the angle brackets, so the pattern cannot distinguish them"
    - "The engine scans right to left and finds the last `>` first"
    - "Without anchors the pattern is applied to the whole string as a single unit"
  answer_index: 0
  explanation: "`.*` first takes everything to the end, then backtracks only as far as it must to let the final `>` match — which lands on the last one. Making it lazy with `<.*?>`, or specific with `<[^>]*>`, matches just `<a>`."

Hands-on exercise

Build one pattern piece by piece, then fall into the greedy trap deliberately. Worked through fully in the Day 38 lab directory.

What you are doingCommand
Match literallygrep -E 'error' app.log
Anchor itgrep -E '^Error' app.log
Whole words onlygrep -E '\bcat\b' words.txt
Match a dategrep -Eo '\d{4}-\d{2}-\d{2}' app.log
Capture and reordersed -E 's/(\d{4})-(\d{2})-(\d{2})/\3\/\2\/\1/'
Fall into the greedy trapecho '<a><b>' | grep -Eo '<.*>'
Then fix itecho '<a><b>' | grep -Eo '<[^>]*>'

Expected output

2026-07-12
12/07/2026
<a><b>
<a>
<b>

Validate your work

Troubleshooting

Common mistakes


Practice assignment


Extension challenge


Quiz

Q1. What does a regular expression describe?

  1. One specific, literal string of characters
  2. The physical layout of characters on a keyboard
  3. A set of strings that all share the same shape or pattern
  4. A compressed version of a text file
Show answer

Answer: C. A set of strings that all share the same shape or pattern

A regex names the rules text must satisfy, so a single pattern like \d+ matches a whole family of strings (7, 42, 1000000) rather than one fixed value.

Q2. Which quantifier means "one or more of the preceding item"?

  1. *
  2. ?
  3. {0,1}
  4. +
Show answer

Answer: D. +

The + quantifier matches one or more repetitions. In contrast, * means zero or more, and ? means zero or one (optional).

Q3. In the pattern ^cat$, what do the ^ and $ do?

  1. They are anchors: ^ is line start, $ is line end
  2. They capture the matched text into groups 1 and 2
  3. They match a literal caret and dollar sign in the text
  4. They make the match case-insensitive
Show answer

Answer: A. They are anchors: ^ is line start, $ is line end

Anchors match positions, not characters. ^ is the start of the line and $ is the end, so ^cat$ matches only a line containing exactly cat and nothing else.

Q4. Why does the pattern <.*> match the entire string <a><b> instead of just <a>?

  1. Because . cannot match the letters a or b
  2. Because .* is greedy and matches as much as possible, running to the last >
  3. Because <.*> is invalid and matches everything by default
  4. Because anchors force it to match the whole line
Show answer

Answer: B. Because .* is greedy and matches as much as possible, running to the last >

Quantifiers are greedy by default, so .* consumes as much text as it can while the pattern still succeeds — here, everything up to the final >. Making it lazy with .*? or using [^>]* fixes it.

Q5. What is the purpose of a capture group written with parentheses, such as (\d{4})?

  1. It makes the pattern run faster
  2. It matches text literally, ignoring any metacharacters inside
  3. It groups a sub-pattern and remembers the text it matched
  4. It marks the end of the regular expression
Show answer

Answer: C. It groups a sub-pattern and remembers the text it matched

A capturing group both applies quantifiers to a sub-pattern and stores the matched text (as \1, \2, and so on) for extraction, reformatting, or backreferences.

Q6. You need to reliably parse deeply nested HTML. Why is a regex the wrong tool?

  1. Regex is too slow to read HTML files
  2. HTML files are too large for any regex engine
  3. Regex only works on numbers, not tags
  4. Regex cannot match nested structure
Show answer

Answer: D. Regex cannot match nested structure

Classic regular expressions provably cannot match arbitrarily nested structure. HTML, JSON, and source code need a format-aware parser; using regex for them produces fragile patterns that break on edge cases.

Q7. On the command line, why do many patterns use [0-9] instead of \d?

  1. \d is not portable across grep and sed dialects
  2. [0-9] is faster in every engine
  3. \d matches letters as well as digits
  4. They mean completely different things
Show answer

Answer: A. \d is not portable across grep and sed dialects

The \d shorthand comes from the PCRE tradition (Python, JavaScript) and is not available in every command-line tool. [0-9] or the POSIX class [[:digit:]] works portably across grep and sed.

Q8. What is catastrophic backtracking (ReDoS) a risk of?

  1. A pattern accidentally deleting the file it searches
  2. Nested quantifiers can force near-exponential work
  3. Regex leaking memory to other programs
  4. A pattern matching the wrong file on disk
Show answer

Answer: B. Nested quantifiers can force near-exponential work

Patterns like (a+)+$ against adversarial input can force a backtracking engine to try an exponential number of paths, so a single request can pin a CPU. Prefer specific classes, avoid nested quantifiers over overlapping classes, and use timeouts.

Glossary

regular expression
A compact string of characters that describes a pattern for matching text — a whole set of strings that share a shape, rather than one literal string.
metacharacter
A character with special meaning in a pattern rather than its literal value, such as . * + ? [ ] ( ) ^ $ | and \, which express classes, repetition, position, and structure.
literal
A character in a pattern that matches itself, such as the a, b, and c in the pattern abc.
character class
A set of characters written in square brackets, any one of which may match; [abc] matches a, b, or c, [a-z] any lowercase letter, and [^0-9] any non-digit.
anchor
A pattern element that matches a position rather than a character: ^ marks the start of a line, $ the end, and \b a word boundary.
quantifier
A symbol that says how many times the preceding item may repeat: * (zero or more), + (one or more), ? (zero or one), and {n,m} (between n and m times).
group
A sub-pattern enclosed in parentheses, which lets a quantifier apply to the whole sub-pattern and, unless made non-capturing, remembers the text it matched.
capture group
A parenthesised group whose matched text the engine stores, numbered left to right (\1, \2, ...), so it can be extracted, reformatted, or referenced later.
alternation
The | operator meaning "this or that"; cat|dog matches either cat or dog, and gr(a|e)y matches gray or grey.
backreference
A reference in a pattern to text an earlier group captured; \1 demands the same text the first group matched, so (\w+) \1 matches a doubled word.
greedy
The default behavior of a quantifier, which matches as much text as possible while still letting the overall pattern succeed — the reason .* often matches too much.
lazy
A quantifier made non-greedy by adding ?, which matches as little text as possible; .*? stops at the first point that lets the rest of the pattern succeed.
PCRE
Perl Compatible Regular Expressions, a widely used C library from 1997 that spread Perl's rich regex syntax to many tools and languages, shaping the modern PCRE-style flavor.
regex engine
The program that takes a pattern and some text and reports whether, and where, the text matches — scanning left to right and backtracking when a choice fails.

Sources and further reading


Kept in this browser, no account needed. Your progress page turns the whole record into one link you can bookmark or open on another device.