04 — Regex¶
Ruby's regex engine is Onigmo. Its syntax is Perl's, with differences in exactly the places that are easy to miss when a pattern moves between languages — anchors, the meaning of /m, the letter \h — and with an engine that has, since Ruby 3.2, defended itself against the most famous way to make a backtracking regex take forever.
| Lesson | The one thing |
|---|---|
^ and $ match at every line |
\A and \z for validation, and a three-language table |
\w is ASCII, [[:alpha:]] is not, and \b sides with Unicode |
why /\b\w+\b/ cannot find café |
\h is a hex digit in Ruby and a blank in Perl |
one letter, three meanings |
| Named captures become local variables — sometimes | the parser rule behind year appearing from nowhere |
| Backtracking has a cache, and a timeout | Regexp.linear_time? and Regexp.timeout |