📄 ch08_05.htm
字号:
<html><head><title>Precedence (Learning Perl, 3rd Edition)</title><link rel="stylesheet" type="text/css" href="../style/style1.css" /><meta name="DC.Creator" content="Randal L. Schwartz and Tom Phoenix" /><meta name="DC.Format" content="text/xml" scheme="MIME" /><meta name="DC.Language" content="en-US" /><meta name="DC.Publisher" content="O'Reilly & Associates, Inc." /><meta name="DC.Source" scheme="ISBN" content="0596001320L" /><meta name="DC.Subject.Keyword" content="stuff" /><meta name="DC.Title" content="Learning Perl, 3rd Edition" /><meta name="DC.Type" content="Text.Monograph" /></head><body bgcolor="#ffffff"><img alt="Book Home" border="0" src="gifs/smbanner.gif" usemap="#banner-map" /><map name="banner-map"><area shape="rect" coords="1,-2,616,66" href="index.htm" alt="Learning Perl, 3rd Edition" /><area shape="rect" coords="629,-11,726,25" href="jobjects/fsearch.htm" alt="Search this book" /></map><div class="navbar"><table width="684" border="0"><tr><td align="left" valign="top" width="228"><a href="ch08_04.htm"><img alt="Previous" border="0" src="../gifs/txtpreva.gif" /></a></td><td align="center" valign="top" width="228"><a href="index.htm"></a></td><td align="right" valign="top" width="228"><a href="ch08_06.htm"><img alt="Next" border="0" src="../gifs/txtnexta.gif" /></a></td></tr></table></div><h2 class="sect1">8.5. Precedence</h2><p><a name="INDEX-566" />Withall of these metacharacters in regular expressions, you may feel thatyou can't keep track of the players without a scorecard.That's the precedence chart, which shows us which parts of thepattern "stick together" the most tightly. Unlike theprecedence chart for operators, the regular expression precedencechart is simple, with only four levels. As a bonus, this section willreview all of the metacharacters that Perl uses in patterns.</p><ol><li><p>At the top of the precedence chart are the parentheses,("<tt class="literal">( )</tt>"), used for grouping and memory.Anything in parentheses will "stick together" moretightly than anything else.</p></li><li><p>The second level is the quantifiers. These are the repeatoperators -- star (<tt class="literal">*</tt>), plus(<tt class="literal">+</tt>), and question mark(<tt class="literal">?</tt>) -- as well as the quantifiers made withcurly braces, like <tt class="literal">{5,15}</tt>,<tt class="literal">{3,}</tt>, and <tt class="literal">{5}</tt>. These alwaysstick to the item they're following.</p></li><li><p>The third level of the precedence chart holds anchors and sequence.The anchors are the caret (<tt class="literal">^</tt>) start-of-stringanchor, the dollar-sign (<tt class="literal">$</tt>) end-of-string anchor,the <tt class="literal">\b</tt> word-boundary anchor, and the<tt class="literal">\B</tt> nonword-boundary anchor. Sequence (putting oneitem after another) is actually an operator, even though itdoesn't use a metacharacter. That means that letters in a wordwill stick together just as tightly as the anchors stick to theletters.</p></li><li><p>The lowest level of precedence is the vertical bar(<tt class="literal">|</tt>) of alternation. Since this is at the bottom ofthe chart, it effectively cuts the pattern into pieces. It's atthe bottom of the chart because we want the letters in the words in<tt class="literal">/fred|barney/</tt> to stick together more tightly thanthe alternation. If alternation were higher priority than sequence,that pattern would mean to match <tt class="literal">fre</tt>, followed bya choice of <tt class="literal">d</tt> or <tt class="literal">b</tt>, followed by<tt class="literal">arney</tt>. So, alternation is at the bottom of thechart, and the letters within the names stick together.</p></li></ol><p>Besides the precedence chart, there are the so-called<em class="firstterm">atoms</em> that make up the most basic pieces ofthe pattern. These are the individual characters, character classes,and backreferences.</p><a name="lperl3-CHP-8-SECT-5.1" /><div class="sect2"><h3 class="sect2">8.5.1. Examples of Precedence</h3><p>When you need to decipher a complex regular expression, you'llneed to do as Perl does, and use the precedence chart to seewhat's really going on.</p><p>For example, <tt class="literal">/^fred|barney$/</tt> is probably not whatthe programmer intended. That's because the vertical bar ofalternation is very low precedence; it cuts the pattern in two. Thatpattern matches either <tt class="literal">fred</tt> at the beginning ofthe string or <tt class="literal">barney</tt> at the end. It's muchmore likely that the programmer wanted<tt class="literal">/^(fred|barney)$/</tt>, which will match if the whole line hasnothing but <tt class="literal">fred</tt>, or nothing but<tt class="literal">barney</tt>.<a href="#FOOTNOTE-187">[187]</a></p><blockquote class="footnote"> <a name="FOOTNOTE-187" /><p>[187]And, perhaps, a newline atthe end of the string, as we mentioned earlier in connection with the<tt class="literal">$</tt> anchor.</p> </blockquote><p>And what will <tt class="literal">/(wilma|pebbles?)/</tt> match? Thequestion mark applies to the previous character,<a href="#FOOTNOTE-188">[188]</a> so thatwill match either <tt class="literal">wilma</tt> or<tt class="literal">pebbles</tt> or <tt class="literal">pebble</tt>, perhaps aspart of a larger string (since there are no anchors).</p><blockquote class="footnote"><a name="FOOTNOTE-188" /><p>[188]Because a quantifier sticks to the letter <tt class="literal">s</tt>more tightly than the <tt class="literal">s</tt> sticks to the otherletters in <tt class="literal">pebbles</tt>.</p> </blockquote><p>The pattern <tt class="literal">/^(\w+)\s+(\w+)$/</tt> matches lines thathave a "word," some required whitespace, and another"word," with nothing else before or after. That might beused to match lines like <tt class="literal">fredflintstone</tt><a name="INDEX-567" />, for example. The parentheses around thewords aren't needed for grouping, so they may be intended tosave those substrings into the regular expression memories, whichwe'll see more about in the next chapter.</p><p>When you're trying to understand a complex pattern, it may behelpful to add parentheses to clarify the precedence. That'sokay, but remember that grouping parentheses are also automaticallymemory parentheses; you may need to change the numbering of othermemories when you add the parentheses.<a href="#FOOTNOTE-189">[189]</a><a name="INDEX-568" /> </p><blockquote class="footnote"> <a name="FOOTNOTE-189" /><p>[189]But look inthe <em class="emphasis">perlre</em> manpage for information aboutnonmemory parentheses, which are used for grouping withoutmemory.</p> </blockquote></div><a name="lperl3-CHP-8-SECT-5.2" /><div class="sect2"><h3 class="sect2">8.5.2. And There's More</h3><p>Although we've covered all of the regular expression featuresthat most people are likely to need for everyday programming, thereare more features being added all the time. Check the<em class="emphasis">perlre</em><a name="INDEX-569" />,<em class="emphasis">perlrequick</em><a name="INDEX-570" />, and<em class="emphasis">perlretut</em><a name="INDEX-571" /> manpages for the latest news about what<a name="INDEX-572" />patterns in Perl can do.<a href="#FOOTNOTE-190">[190]</a></p><blockquote class="footnote"><a name="FOOTNOTE-190" /><p>[190]And check out <tt class="literal">YAPE::Regexp::Explain</tt> in CPANas a regular-expression-to-English translator.</p> </blockquote></div><hr width="684" align="left" /><div class="navbar"><table width="684" border="0"><tr><td align="left" valign="top" width="228"><a href="ch08_04.htm"><img alt="Previous" border="0" src="../gifs/txtpreva.gif" /></a></td><td align="center" valign="top" width="228"><a href="index.htm"><img alt="Home" border="0" src="../gifs/txthome.gif" /></a></td><td align="right" valign="top" width="228"><a href="ch08_06.htm"><img alt="Next" border="0" src="../gifs/txtnexta.gif" /></a></td></tr><tr><td align="left" valign="top" width="228">8.4. Memory Parentheses</td><td align="center" valign="top" width="228"><a href="index/index.htm"><img alt="Book Index" border="0" src="../gifs/index.gif" /></a></td><td align="right" valign="top" width="228">8.6. Exercises</td></tr></table></div><hr width="684" align="left" /><img alt="Library Navigation Links" border="0" src="../gifs/navbar.gif" usemap="#library-map" /><p><p><font size="-1"><a href="copyrght.htm">Copyright © 2002</a> O'Reilly & Associates. All rights reserved.</font></p><map name="library-map"><area shape="rect" coords="1,0,85,94" href="../index.htm"><area shape="rect" coords="86,1,178,103" href="../lwp/index.htm"><area shape="rect" coords="180,0,265,103" href="../lperl/index.htm"><area shape="rect" coords="267,0,353,105" href="../perlnut/index.htm"><area shape="rect" coords="354,1,446,115" href="../prog/index.htm"><area shape="rect" coords="448,0,526,132" href="../tk/index.htm"><area shape="rect" coords="528,1,615,119" href="../cookbook/index.htm"><area shape="rect" coords="617,0,690,135" href="../pxml/index.htm"></map></body></html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -