home | O'Reilly's CD bookshelfs | FreeBSD | Linux | Cisco | Cisco Exam  

Book HomeLearning Perl, 3rd EditionSearch this book

8.6. Exercises

See Section A.7, "Answers to Chapter 8 Exercises" for answers to the following exercises. These exercises are among the most challenging in the entire book. But don't get too discouraged! The following chapters will actually be easier, partly because you'll have the power of regular expressions to help you.

  1. [4] Using the test program from the previous chapter, make a pattern that matches only lines containing either the word fred or wilma, followed by some whitespace, and then the word flintstone. So it should match the string I am fred flintstone (with one or more spaces or tabs between the names).

  2. [10] Here, we give you the answer; you decide what problem it's trying to solve. What do these real-world patterns match? What might they be used for?


    Try each of them in the test program. It may help to find some strings that match (and that fail to match) each one.

  3. [8] Make a pattern that will match a string containing nothing but a scalar variable's name (not its value!), like $fred, $barney, or $_ (but you shouldn't match special variables like $0). That is, if the line of input has the six characters $wilma, the pattern should match. If the input says wilma, it should not match.

  4. [12] Make a pattern that matches any line of input that has the same word repeated two or more times in a row. Words in this problem can be considered to be sequences of letters a to z or A to Z, digits, and underscores. Whitespace between words may differ. For example, the classic observation-test string Paris in the the spring should match, since it has a doubled word. Also, I thought that that was the problem should match, even though that may be a correct use of a doubled word. Does your pattern match all three words in I thought that that that was the problem (with extra spaces between only some of the words)? Does it match This is a test? How about This shouldn't match, according to the theory of regular expressions?

Library Navigation Links

Copyright © 2002 O'Reilly & Associates. All rights reserved.