More Powerful Regular Expressions
After reading (almost) three chapters about regular expressions, you know they’re a powerful feature in the core of Perl. The Perl developers have added more features, and you’ll see some of the most important ones in this section. At the same time, you’ll see a little more about the internal operation of the regular expression engine.
Non-Greedy Quantifiers
The four quantifiers you’ve already seen (in Chapters 7 and 8) are all greedy
. That means they match as much as they can, reluctantly giving some back if that’s necessary to allow the overall pattern to succeed. Here’s an example: Suppose you’re using the pattern /fred.+barney/
on the string fred and barney went bowling last night
. We know that the regular expression will match that string, but let’s see how it goes about it.[223] First, the subpattern fred
matches the identical literal string. The next part of the pattern is the .+
, which matches any character except newline, at least one time. But the plus quantifier is greedy; it prefers to match as much as possible. So, it immediately matches all of the rest of the string, including the word night
. (This may surprise you, but the story isn’t over yet.)
Now the subpattern barney
would like to match, but it can’t because we’re at the end of the string. Since the .+
could be successful even if it matched one fewer character, it reluctantly gives back the letter t
at the end of the string. (It’s greedy, but it wants the whole pattern to succeed ...
Get Learning Perl, Fourth 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.