📄 ch09_01.htm
字号:
<html><head><title>Using Regular Expressions (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_06.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="ch09_02.htm"><img alt="Next" border="0" src="../gifs/txtnexta.gif" /></a></td></tr></table></div>
<h1 class="chapter">Chapter 9. Using Regular Expressions</h1>
<div class="htmltoc"><h4 class="tochead">Contents:</h4>
<p> <a href="#lperl3-CHP-9-SECT-1">Matches with m//</a><br />
<a href="ch09_02.htm">Option Modifiers</a><br />
<a href="ch09_03.htm">The Binding Operator, =~</a><br />
<a href="ch09_04.htm">Interpolating into Patterns</a><br />
<a href="ch09_05.htm">The Match Variables</a><br />
<a href="ch09_06.htm">Substitutions with s///</a><br />
<a href="ch09_07.htm">The split Operator</a><br />
<a href="ch09_08.htm">The join Function</a><br />
<a href="ch09_09.htm">Exercises</a><br /></p></div>
<p><a name="INDEX-573" /></a>Now that
we've seen what goes inside a regular expression, let's
take what we've learned back into Perl.
</p>
<div class="sect1"><a name="lperl3-CHP-9-SECT-1" /></a>
<h2 class="sect1">9.1. Matches with m//</h2>
<p>We've been writing patterns in pairs of forward slashes, like
<tt class="literal">/fred/</tt>. But this is actually a shortcut for the
<tt class="literal">m//</tt><a name="INDEX-574" /></a> <a name="INDEX-575" /></a>
<a name="INDEX-576" /></a> (pattern match) operator. As we saw
with the <tt class="literal">qw//</tt> operator, you may choose any pair of
delimiters to quote the contents. So, we could write that same
expression as <tt class="literal">m(fred)</tt>,
<tt class="literal">m<fred></tt>, <tt class="literal">m{fred}</tt>, or
<tt class="literal">m[fred]</tt> using those paired delimiters, or as
<tt class="literal">m,fred,</tt>, <tt class="literal">m!fred!</tt>,
<tt class="literal">m^fred^</tt>, or many other ways using nonpaired
delimiters.<a href="#FOOTNOTE-191">[191]</a>
</p><blockquote class="footnote"> <a name="FOOTNOTE-191" /></a><p>[191]Nonpaired
<a name="INDEX-577" /></a>
<a name="INDEX-578" /></a>delimiters are the ones that
don't have a different "left" and
"right" variety; the same punctuation mark is used for
both ends.</p> </blockquote>
<p>The shortcut is that if you choose the forward slash as the
delimiter, you may omit the initial <tt class="literal">m</tt>. Since Perl
folks love to avoid typing extra characters, you'll see most
pattern matches written using <a name="INDEX-579" /></a> <a name="INDEX-580" /></a>slashes, as in
<tt class="literal">/fred/</tt>.
</p>
<p>Of course, you should wisely choose a delimiter that doesn't
appear in your pattern.<a href="#FOOTNOTE-192">[192]</a> If you wanted to make a pattern to match the beginning of
an ordinary web URL, you might start to write
<tt class="literal">/^http:\/\//</tt> to match the initial
<tt class="literal">"http://"</tt>. But that's easier to read, write,
maintain, and debug if you use a better choice of delimiter:
<tt class="literal">m%^http://%</tt>.<a href="#FOOTNOTE-193">[193]</a>
</p><blockquote class="footnote"> <a name="FOOTNOTE-192" /></a><p>[192]If you're using paired
delimiters, you shouldn't generally have to worry about using
the delimiter inside the pattern, since that delimiter will generally
be paired inside your pattern. That is,
<tt class="literal">m(fred(.*)barney)</tt> and <tt class="literal">m{\w{2,}}</tt>
and <tt class="literal">m[wilma[\n \t]+betty]</tt> are all fine, even
though the pattern contains the quoting character, since each
"left" has a corresponding "right". But the
angle brackets ("<tt class="literal"><</tt>" and
"<tt class="literal">></tt>") aren't regular
expression metacharacters, so they may not be paired; if the pattern
were <tt class="literal">m{(\d+)\s*>=?\s*(\d+)}</tt>, quoting it with
angle brackets would mean having to backslash the greater-than sign
so that it wouldn't prematurely end the pattern.</p>
</blockquote><blockquote class="footnote"> <a name="FOOTNOTE-193" /></a><p>[193]Remember, the forward
slash is not a metacharacter, so it doesn't need to be
backslashed when it's not the delimiter.</p> </blockquote>
<p>It's common to use curly braces as the delimiter. If you use a
programmers' text editor, it probably has the ability to jump
from an opening curly brace to the corresponding closing one, which
can be handy in maintaining code.
</p>
</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_06.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="ch09_02.htm"><img alt="Next" border="0" src="../gifs/txtnexta.gif" /></a></td></tr><tr><td align="left" valign="top" width="228">8.6. Exercises</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">9.2. Option Modifiers</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 + -