With all of these metacharacters in regular expressions, you may feel that you can't keep track of the players without a scorecard. That's the precedence chart, which shows us which parts of the pattern "stick together" the most tightly. Unlike the precedence chart for operators, the regular expression precedence chart is simple, with only four levels. As a bonus, this section will review all of the metacharacters that Perl uses in patterns.
Besides the precedence chart, there are the so-called atoms that make up the most basic pieces of the pattern. These are the individual characters, character classes, and backreferences.
8.5.1. Examples of Precedence
When you need to decipher a complex regular expression, you'll need to do as Perl does, and use the precedence chart to see what's really going on.
For example, /^fred|barney$/ is probably not what the programmer intended. That's because the vertical bar of alternation is very low precedence; it cuts the pattern in two. That pattern matches either fred at the beginning of the string or barney at the end. It's much more likely that the programmer wanted /^(fred|barney)$/, which will match if the whole line has nothing but fred, or nothing but barney.
And what will /(wilma|pebbles?)/ match? The question mark applies to the previous character, so that will match either wilma or pebbles or pebble, perhaps as part of a larger string (since there are no anchors).
The pattern /^(\w+)\s+(\w+)$/ matches lines that have a "word," some required whitespace, and another "word," with nothing else before or after. That might be used to match lines like fred flintstone, for example. The parentheses around the words aren't needed for grouping, so they may be intended to save those substrings into the regular expression memories, which we'll see more about in the next chapter.
When you're trying to understand a complex pattern, it may be helpful to add parentheses to clarify the precedence. That's okay, but remember that grouping parentheses are also automatically memory parentheses; you may need to change the numbering of other memories when you add the parentheses.
8.5.2. And There's More
Although we've covered all of the regular expression features that most people are likely to need for everyday programming, there are more features being added all the time. Check the perlre, perlrequick, and perlretut manpages for the latest news about what patterns in Perl can do.
Copyright © 2002 O'Reilly & Associates. All rights reserved.