More Powerful Regular Expressions
After already reading three (almost!) chapters about regular expressions, you know that they’re a powerful feature in the core of Perl. But there are even more features that the Perl developers have added; 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.
Nongreedy Quantifiers
The four quantifiers you’ve already seen (in Chapters 7 and 8) are all
greedy. That means that they match as much as
they can, only to reluctantly give 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
. Of
course, we know that the regular expression will match that string,
but let’s see how it goes about it.[*] First, of course, 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—we’re at the end of the string. But
since the .+
could still be
successful even if it matched one fewer character, it reluctantly
gives back the letter t
at the end of the ...
Get Learning Perl, 5th 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.