skipnothing/Regex Engines

Recursive Descent & Capture

Run (a|ab)c against the string abc. The first parentheses grab a, the match stalls one character later, and the engine quietly swaps that a for ab before the pattern finally lands.

  • ▸A group fills the moment the engine leaves the parentheses, and a later failure can erase it.
  • ▸Back up past a group and it gives up its match, rewound with the engine's position.
  • ▸Repeat a group and only the last pass survives in its slot; move one parenthesis and a different span wins.
01

Descend, Fail, Back Up

Hand the pattern cat|car to the engine and point it at the input car. It does not weigh both branches at once. It writes down where it is, position zero, and commits to the first branch, cat. It reads c, a, then wants t while the input offers r. Dead end. Instead of quitting, the engine throws away the two characters of progress it just made and jumps back to the position it wrote down, then tries the next branch, car, from there, which matches to the end. That walk, pick one option, descend, and on failure rewind to the last marked spot to try the next, is recursive descent. Every place the pattern offers more than one road is a choice point, and the engine meets them strictly one at a time.

WALK THE ENGINE INTO A DEAD END AND BACK

Keep going, sign up to unlock the rest

3 more parts in this topic, plus 11+ more topics in Regex Engines.

Sign up, it's freeSee the full Regex Engines
Backtracking & Catastrophe0/3#7 DFA vs NFA Engines
#9 Backreferences