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]
-
[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?
-
[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?
-
[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.)
-
[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.
-
[8] Extra credit exercise: write a program that prints out any input
line that mentions both wilma
and fred.
| | | 7.3. A Pattern Test Program | | 8. More About Regular Expressions |
Copyright © 2002 O'Reilly & Associates. All rights reserved.
|
|