Chapter 7. Concepts of Regular Expressions

Perl has many features that set it apart from other languages. Of all those features, one of the most important is its strong support for regular expressions. These allow fast, flexible, and reliable string handling.

But that power comes at a price. Regular expressions are actually tiny programs in their own special language, built inside Perl. (Yes, you’re about to learn another programming language![1] Fortunately it’s a simple one.) So for the next two chapters, we’ll be learning that language; then we’ll take what we’ve learned back to the world of Perl in Chapter 9.

Regular expressions aren’t merely part of Perl; they’re also found in sed and awk, procmail, grep, most programmers’ text editors like vi and emacs, and even in more esoteric places. If you’ve seen some of these already, you’re ahead of the game. Keep watching, and you’ll see many more tools that use or support regular expressions, such as search engines on the Web (often written in Perl), email clients, and others.

What Are Regular Expressions?

A regular expression, often called a pattern in Perl, is a template that either matches or doesn’t match a given string.[2] That is, there are an infinite number of possible text strings; a given pattern divides that infinite set into two groups: the ones that match, and the ones that don’t. There’s never any kinda-sorta-almost-up-to-here wishy-washy matching: either it matches or it doesn’t. A pattern may match just one possible ...

Get Learning Perl, 3rd Edition 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.