build-a-regex-engine / lesson-15.md
Lesson 15 · Groups, classes & repetition

Escaping metacharacters

A backslash before a metacharacter strips its power - `a\.c` matches a real dot, not any character. This is how a pattern matches the very symbols regex uses for syntax.

The goal

Parse a backslash followed by a metacharacter into a Literal for that character.

Start here - the target
TO DO
Scenario: A backslash turns a metacharacter into a literal
Giventhe pattern 'a\.c'
WhenMatch is called against "a.c"
Thenit reports true, and 'a\.c' against "axc" reports false
AndMatch for 'a\*b' against "a*b" reports true
AndMatch for 'a\*b' against "aaab" reports false
Background

Regex needs an escape hatch: how do you match a literal . when . means “any character”? You escape it. A backslash followed by a metacharacter - \., \*, \(, \\ - produces a Literal of that exact byte, with no special meaning. Now a pattern can match the punctuation that regex itself is built from.

This extends the same backslash branch you opened yesterday for \d, \w, \s. The rule is simple: after a \, a handful of letters mean shorthand classes, and everything else means “the next byte, taken literally”. That single dispatch point - look at the character after the backslash - now covers both jobs. With escaping in place, the only quantifier left is the counted kind, {n}, which the last two lessons of the chapter add.

Make it work
// Extend the backslash branch from yesterday. After a '\':
// d, w, s -> the shorthand classes
// anything else -> a Literal of that exact byte
// So '\.', '\*', '\(', '\\' all become plain characters.
CheckpointDONE
A backslash escapes metacharacters into literals. Commit and stop here.