build-a-regex-engine / lesson-03.md
Lesson 03 · A matcher you can call

Searching anywhere in the text

A real regex finds a match anywhere in the text, not just at the start. Today you wrap yesterday's prefix check in a search loop and expose your first public function, Match.

The goal

Add Match, which reports true if the pattern matches starting at any position in the text.

Start here - the target
TO DO
Scenario: Searching for a match at any position
Giventhe pattern "bcd"
WhenMatch is called against "abcde"
Thenit reports true
AndMatch for "xyz" against "abcde" reports false
AndMatch for "" against "abc" reports true
Background

Yesterday’s matchHere only looked at the front of the text. A search engine has to consider every starting position: bcd is in abcde, just not at index 0. So Match tries matchHere at position 0, then 1, then 2, and reports true the first time one succeeds. This unanchored, match-anywhere behavior is the default your whole library will follow - tomorrow’s ^ is how a user opts out of it.

Note the loop runs to len(text) inclusive, not exclusive: an empty pattern, and later the $ anchor, need a chance to match at the very end where no bytes remain. Match is now your public surface - a caller passes a pattern and some text and gets a yes or no. Everything from here just makes that yes-or-no smarter.

Make it work
// try matchHere at position 0, then 1, then 2 ... up to the end.
// remember to try the empty tail so "" and "$" can match.
func Match(pat, text string) bool {
nodes := parse(pat).Nodes
// for i := 0; i <= len(text); i++ { ... }
}
CheckpointDONE
Match searches the whole text for the pattern. Your engine has a public entry point. Commit and stop here.