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

Book HomeLearning Perl, 3rd EditionSearch this book

7.4. Exercises

See Section A.6, "Answers to Chapter 7 Exercises" for answers to the following exercises:

Remember, it's normal to be surprised by some of the things that regular expressions do; that's one reason that the exercises in this chapter are even more important than the others. Expect the unexpected.

Several of these exercises ask you to use the test program from this chapter. You could manually type up this program, taking great care to get all of the odd punctuation marks correct.[171] But you'll probably find it faster and easier to simply download the program and some other goodies from the O'Reilly website, as we mentioned in the Preface. You'll find this program under the name pattern_test.[172]

[171]If you do type it up on your own, remember that the backtick character (`) is not the same as the apostrophe ('). On most full-sized computer keyboards these days (in the U.S., at least), the backtick is found on a key immediately to the left of the 1 key. Try out the program with the pattern /match/and the string beforematchafter, as the text describes, and see that it works correctly before you do the exercises.

[172]Don't be surprised if the program you download is a little fancier than what we have in the book. The commented-out extra features you see will come in handy in later exercises.

  1. [6] Use the test program to make and test a pattern that matches any string containing fred. Does it match if your string is Fred, frederick, or Alfred?

  2. [6] Use the test program to make and test a pattern that matches any string containing at least one a followed by any number of b's. Remember that "any number" might be zero. Does it match if your string is barney, fred, abba, or dinosaur?

  3. [5] Use the test program to make and test a pattern that matches any string containing any number of backslashes followed by any number of asterisks. Does it match if your string is \\**, fred, barney \\\***, or *wilma\? (Note the typography; those are four separate test strings.)

  4. [6] Write a new program (not the test program) that prints out any input line that mentions wilma. (Any other lines should simply be skipped.) For extra credit, allow it to match Wilma with a capital W as well.

  5. [8] Extra credit exercise: write a program that prints out any input line that mentions both wilma and fred.



Library Navigation Links

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











??????????????@Mail.ru