📄 ch07_04.htm
字号:
<html><head><title>Exercises (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="ch07_03.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_01.htm"><img alt="Next" border="0" src="../gifs/txtnexta.gif" /></a></td></tr></table></div><h2 class="sect1">7.4. Exercises</h2><p>See <a href="appa_06.htm#lperl3-APP-A-SECT-6">Section A.6, "Answers to Chapter 7 Exercises"</a> for answers to the followingexercises:</p><p>Remember, it's normal to be surprised by some of the thingsthat regular expressions do; that's one reason that theexercises in this chapter are even more important than the others.Expect the unexpected.</p><p>Several of these exercises ask you to use the test program from thischapter. You <em class="emphasis">could</em> manually type up thisprogram, taking great care to get all of the odd punctuation markscorrect.<a href="#FOOTNOTE-171">[171]</a> But you'll probably find it faster and easier tosimply <a name="INDEX-523" />download the program and some othergoodies from the O'Reilly website, as we mentioned in thePreface. You'll find this program under the name<em class="filename">pattern_test</em>.<a href="#FOOTNOTE-172">[172]</a></p><blockquote class="footnote"> <a name="FOOTNOTE-171" /><p>[171]If you <em class="emphasis">do</em> type it up onyour own, remember that the backtick character (<tt class="literal">`</tt>)is not the same as the apostrophe (<tt class="literal">'</tt>). On mostfull-sized computer keyboards these days (in the U.S., at least), thebacktick is found on a key immediately to the left of the<tt class="literal">1</tt> key. Try out the program with the pattern<tt class="literal">/match/</tt>and the string<tt class="literal">beforematchafter</tt>, as the text describes, and seethat it works correctly before you do the exercises.</p></blockquote><blockquote class="footnote"> <a name="FOOTNOTE-172" /><p>[172]Don't besurprised if the program you download is a little fancier than whatwe have in the book. The commented-out extra features you see willcome in handy in later exercises.</p> </blockquote><ol><li><p>[6] Use the test program to make and test a pattern that matches anystring containing <tt class="literal">fred</tt>. Does it match if yourstring is <tt class="literal">Fred</tt>, <tt class="literal">frederick</tt>, or<tt class="literal">Alfred</tt>?</p></li><li><p>[6] Use the test program to make and test a pattern that matches anystring containing at least one <tt class="literal">a</tt> followed by anynumber of <tt class="literal">b</tt>'s. Remember that "anynumber" might be zero. Does it match if your string is<tt class="literal">barney</tt>, <tt class="literal">fred</tt>,<tt class="literal">abba</tt>, or <tt class="literal">dinosaur</tt>?</p></li><li><p>[5] Use the test program to make and test a pattern that matches anystring containing any number of backslashes followed by any number ofasterisks. Does it match if your string is <tt class="literal">\\**</tt>,<tt class="literal">fred</tt>, <tt class="literal">barney \\\***</tt>, or<tt class="literal">*wilma\</tt>? (Note the typography; those are fourseparate test strings.)</p></li><li><p>[6] Write a new program (<em class="emphasis">not</em> the test program)that prints out any input line that mentions<tt class="literal">wilma</tt>. (Any other lines should simply be skipped.)For extra credit, allow it to match <tt class="literal">Wilma</tt> with acapital <tt class="literal">W</tt> as well.</p></li><li><p>[8] Extra credit exercise: write a program that prints out any inputline that mentions <em class="emphasis">both</em> <tt class="literal">wilma</tt>and <tt class="literal">fred</tt>.</p></li></ol><hr width="684" align="left" /><div class="navbar"><table width="684" border="0"><tr><td align="left" valign="top" width="228"><a href="ch07_03.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_01.htm"><img alt="Next" border="0" src="../gifs/txtnexta.gif" /></a></td></tr><tr><td align="left" valign="top" width="228">7.3. A Pattern Test Program</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. More About Regular Expressions</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 + -