ch08_06.htm
来自「by Randal L. Schwartz and Tom Phoenix I」· HTM 代码 · 共 83 行
HTM
83 行
<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="ch08_05.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_01.htm"><img alt="Next" border="0" src="../gifs/txtnexta.gif" /></a></td></tr></table></div><h2 class="sect1">8.6. Exercises</h2><p>See <a href="appa_07.htm#lperl3-APP-A-SECT-7">Section A.7, "Answers to Chapter 8 Exercises"</a> for answers to the followingexercises. These exercises are among the most challenging in theentire book. But don't get too discouraged! The followingchapters will actually be easier, partly because you'll havethe power of regular expressions to help you.</p><ol><li><p>[4] Using the test program from the previous chapter, make a patternthat matches only lines containing either the word<tt class="literal">fred</tt> or <tt class="literal">wilma</tt>, followed by somewhitespace, and then the word <tt class="literal">flintstone</tt>. So itshould match the string <tt class="literal">I am fred flintstone</tt> (withone or more spaces or tabs between the names).</p></li><li><p>[10] Here, we give you the answer; you decide what problem it'strying to solve. What do these real-world patterns match? What mightthey be used for?</p><blockquote><pre class="code">/"([^"]*)"//^0?[0-3]?[0-7]{1,2}$//^\b[\w.]{1,12}\b$/</pre></blockquote><p>Try each of them in the test program. It may help to find somestrings that match (and that fail to match) each one.</p></li><li><p>[8] Make a pattern that will match a string containing nothing but ascalar variable's name (not its value!), like<tt class="literal">$fred</tt>, <tt class="literal">$barney</tt>, or<tt class="literal">$_</tt> (but you shouldn't match specialvariables like <tt class="literal">$0</tt>). That is, if the line of inputhas the six characters <tt class="literal">$wilma</tt>, the pattern shouldmatch. If the input says <tt class="literal">wilma</tt>, it should notmatch.</p></li><li><p>[12] Make a pattern that matches any line of input that has the sameword repeated two or more times in a row. Words in this problem canbe considered to be sequences of letters <tt class="literal">a</tt> to<tt class="literal">z</tt> or <tt class="literal">A</tt> to <tt class="literal">Z</tt>,digits, and underscores. Whitespace between words may differ. Forexample, the classic observation-test string <tt class="literal">Paris in thethe spring</tt> should match, since it has a doubled word. Also,<tt class="literal">I thought that that was the problem</tt> should match,even though that may be a correct use of a doubled word. Does yourpattern match all <em class="emphasis">three</em> words in <tt class="literal">Ithought that that that was the problem</tt> (with extra spacesbetween only some of the words)? Does it match <tt class="literal">This isa</tt> <tt class="literal">test</tt>? How about <tt class="literal">Thisshouldn't match, according to the theory</tt> <tt class="literal">ofregular expressions</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="ch08_05.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_01.htm"><img alt="Next" border="0" src="../gifs/txtnexta.gif" /></a></td></tr><tr><td align="left" valign="top" width="228">8.5. Precedence</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. Using 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 + =
减小字号Ctrl + -
显示快捷键?