⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ch05_07.htm

📁 编程珍珠,里面很多好用的代码,大家可以参考学习呵呵,
💻 HTM
📖 第 1 页 / 共 2 页
字号:
print "postmatch: $'\n";          # , and enter.print "lastmatch: $+\n";          # &lt;/EM&gt;</pre></blockquote>For more explanation of these magical Elvish variables (and for a wayto write them in English), see<a href="ch28_01.htm">Chapter 28, "Special Names"</a>.</p><p><a name="INDEX-1654"></a><a name="INDEX-1655"></a>The <tt class="literal">@-</tt> (<tt class="literal">@LAST_MATCH_START</tt>) array holds the offsets of thebeginnings of any submatches, and <tt class="literal">@+</tt> (<tt class="literal">@LAST_MATCH_END</tt>) holds theoffsets of the ends:<blockquote><pre class="programlisting">#!/usr/bin/perl$alphabet = "abcdefghijklmnopqrstuvwxyz";$alphabet =~ /(hi).*(stu)/;print "The entire match began at $-[0] and ended at $+[0]\n";print "The first  match began at $-[1] and ended at $+[1]\n";print "The second match began at $-[2] and ended at $+[2]\n";</pre></blockquote></p><p>If you really want to match a literal parenthesis character instead of havingit interpreted as a metacharacter, backslash it:<blockquote><pre class="programlisting">/\(e.g., .*?\)/</pre></blockquote>This matches a parenthesized example (e.g., this statement).  But since dotis a wildcard, this also matches any parenthetical statement with thefirst letter <tt class="literal">e</tt> and third letter <tt class="literal">g</tt> (ergo, this statement too).</p><a name="INDEX-1656"></a><a name="INDEX-1657"></a><h3 class="sect2">5.7.2. Clustering</h3><a name="INDEX-1658"></a><a name="INDEX-1659"></a><p><a name="INDEX-1660"></a><a name="INDEX-1661"></a>Bare parentheses both cluster and capture.  But sometimes you don'twant that.  Sometimes you just want to group portions of the patternwithout creating a backreference.  You can use an extended form ofparentheses to suppress capturing: the <tt class="literal">(?:</tt><em class="replaceable">PATTERN</em><tt class="literal">)</tt> notationwill <em class="emphasis">cluster</em> without capturing.</p><p><a name="INDEX-1662"></a>There are at least three reasons you might want to cluster withoutcapturing:</p><ol><li><p>To quantify something.</p></li><li><p><a name="INDEX-1663"></a>To limit the scope of interior alternation; for example,<tt class="literal">/^cat|cow|dog$/</tt> needs to be <tt class="literal">/^(?:cat|cow|dog)$/</tt> so that thecat doesn't run away with the <tt class="literal">^</tt>.</p></li><li><p>To limit the scope of an embedded pattern modifier to a particularsubpattern, such as in <tt class="literal">/foo(?-i:Case_Matters)bar/i</tt>.(See the next section, <a href="ch05_07.htm#ch05-sect-cloist">Section 5.7.3, "Cloistered Pattern Modifiers"</a>)</p></li></ol><p>In addition, it's more efficient to suppress the capture of somethingyou're not going to use.  On the minus side, the notation is a littlenoisier, visually speaking.</p><p><a name="INDEX-1664"></a>In a pattern,a left parenthesis immediately followed by a question mark denotes aregex <em class="emphasis">extension</em>.  The current regular expressionbestiary is relatively fixed--we don't dare create a newmetacharacter, for fear of breaking old Perl programs.  Instead, theextension syntax is used to add new features to the bestiary.</p><p>In the remainder of the chapter, we'll see manymore regex extensions, all of which cluster without capturing, as wellas doing something else.  The<tt class="literal">(?:</tt><em class="replaceable">PATTERN</em><tt class="literal">)</tt>extension is just special in that it does nothing else.  So if yousay:<blockquote><pre class="programlisting">@fields = split(/\b(?:a|b|c)\b/)</pre></blockquote>it's like:<blockquote><pre class="programlisting">@fields = split(/\b(a|b|c)\b/)</pre></blockquote><a name="INDEX-1665"></a>but doesn't spit out extra fields.  (The <tt class="literal">split</tt> operator is a bitlike <tt class="literal">m//g</tt> in that it will emit extra fields for all the capturedsubstrings within the pattern.  Ordinarily, <tt class="literal">split</tt> only returns what it<em class="emphasis">didn't</em> match.  For more on <tt class="literal">split</tt> see <a href="ch29_01.htm">Chapter 29, "Functions"</a>.)</p><a name="ch05-sect-cloist"></a><h3 class="sect2">5.7.3. Cloistered Pattern Modifiers</h3><p><a name="INDEX-1666"></a><a name="INDEX-1667"></a><a name="INDEX-1668"></a>You may <em class="emphasis">cloister</em> the <tt class="literal">/i</tt>, <tt class="literal">/m</tt>, <tt class="literal">/s</tt>, and <tt class="literal">/x</tt> modifierswithin a portion of your pattern by inserting them (without theslash) between the <tt class="literal">?</tt> and <tt class="literal">:</tt> of the clustering notation.  If you say:<blockquote><pre class="programlisting">/Harry (?i:s) Truman/</pre></blockquote>it matches both "<tt class="literal">Harry S Truman</tt>" and"<tt class="literal">Harry s Truman</tt>", whereas:<blockquote><pre class="programlisting">/Harry (?x: [A-Z] \.? \s )?Truman/</pre></blockquote>matches both "<tt class="literal">Harry S Truman</tt>" and "<tt class="literal">HarryS. Truman</tt>", as well as "<tt class="literal">Harry Truman</tt>", and:<blockquote><pre class="programlisting">/Harry (?ix: [A-Z] \.? \s )?Truman/</pre></blockquote>matches all five, by combining the <tt class="literal">/i</tt> and<tt class="literal">/x</tt> modifiers within the cloister.</p><p><a name="INDEX-1669"></a>You can also subtract modifiers from a cloister with a minus sign:<blockquote><pre class="programlisting">/Harry (?x-i: [A-Z] \.? \s )?Truman/i</pre></blockquote>This matches any capitalization of the name--but if the middle initialis provided, it must be capitalized, since the <tt class="literal">/i</tt>applied to the overall pattern is suspended inside the cloister.</p><p>By omitting the colon and <em class="replaceable">PATTERN</em>, you can export modifier settingsto an outer cluster, turning it into a cloister.  That is, you canselectively turn modifiers on and off for the cluster one level outsidethe modifiers' parentheses, like so:<blockquote><pre class="programlisting">/(?i)foo/            # Equivalent to /foo/i/foo((?-i)bar)/i     # "bar" must be lower case/foo((?x-i) bar)/    # Enables /x and disables /i for "bar"</pre></blockquote>Note that the second and third examples create backreferences.If that wasn't what you wanted, then you should have been using<tt class="literal">(?-i:bar)</tt> and <tt class="literal">(?x-i: bar)</tt>, respectively.</p><p>Setting modifiers on a portion of your pattern is particularly usefulwhen you want "<tt class="literal">.</tt>" to match newlines in part of your pattern butnot in the rest of it.  Setting <tt class="literal">/s</tt> on the whole pattern doesn't helpyou there.</p><!-- BOTTOM NAV BAR --><hr width="515" align="left"><div class="navbar"><table width="515" border="0"><tr><td align="left" valign="top" width="172"><a href="ch05_06.htm"><img src="../gifs/txtpreva.gif" alt="Previous" border="0"></a></td><td align="center" valign="top" width="171"><a href="index.htm"><img src="../gifs/txthome.gif" alt="Home" border="0"></a></td><td align="right" valign="top" width="172"><a href="ch05_08.htm"><img src="../gifs/txtnexta.gif" alt="Next" border="0"></a></td></tr><tr><td align="left" valign="top" width="172">5.6. Positions</td><td align="center" valign="top" width="171"><a href="index/index.htm"><img src="../gifs/index.gif" alt="Book Index" border="0"></a></td><td align="right" valign="top" width="172">5.8. Alternation</td></tr></table></div><hr width="515" align="left"><!-- LIBRARY NAV BAR --><img src="../gifs/smnavbar.gif" usemap="#library-map" border="0" alt="Library Navigation Links"><p><font size="-1"><a href="copyrght.htm">Copyright &copy; 2001</a> O'Reilly &amp; Associates. All rights reserved.</font></p><map name="library-map"> <area shape="rect" coords="2,-1,79,99" href="../index.htm"><area shape="rect" coords="84,1,157,108" href="../perlnut/index.htm"><area shape="rect" coords="162,2,248,125" href="../prog/index.htm"><area shape="rect" coords="253,2,326,130" href="../advprog/index.htm"><area shape="rect" coords="332,1,407,112" href="../cookbook/index.htm"><area shape="rect" coords="414,2,523,103" href="../sysadmin/index.htm"></map><!-- END OF BODY --></body></html>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -