Chapter 13. Understanding Regular Expressions, Part I

Jeffrey Friedl

I’d like to tell you a story about a friend of mine, Fred. Because of the nature of the story, I talk a lot about Perl regular expressions and how they work behind the scenes. I assume you know Perl at least as well as Fred. That’s not saying a great deal, since he doesn’t know it all that well, although he thinks he does.

Regular expressions need not be difficult, and need not be a mystery. Frankly, if you put aside all the theoretical mumbo-jumbo and look from a practical point of view how the Perl regular-expression engine works, then you too can think along those lines and know how an expression will act in any given situation.

So, I believe that with a bit of explanation and a healthy dose of experience, you’ll become an undaunted expert. This story covers the whole range, from the Fred level all the way up to the expert level.

Note that the approach I take for explaining regular expressions is perhaps quite different than what you or Fred have seen before. The story approaches Perl regular expressions “from the back.” Documentation tends to provide a raw “these metacharacters do such-and-such” table along with a few examples. Rather than rehashing that old story, I’ll present the regular-expression engine’s point of view, showing what it actually does when attempting a match. Eventually, we will work our way “to the front” to see what relation these workings have to the metacharacters you feed it. It’s a longer ...

Get Computer Science & Perl Programming now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.