Exercises
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.[*] But you’ll probably find it faster and easier to simply download the program and some other goodies from the O’Reilly web site, as we mentioned in the Preface. You’ll find this program under the name pattern_test.[†]
See Appendix A for answers to the following exercises:
[8] Using the pattern test program, make a pattern to match the string
match. Try the program with the input stringbeforematchafter. Does the output show the three parts of the match in the right order?[7] Using the pattern test program, make a pattern that matches if any word (in the
\wsense of word) ends with the lettera. Does it matchwilmabut notbarney? Does it matchMrs. Wilma Flintstone? What aboutwilma&fred? Try it on the sample text file from the previous chapter’s exercises (and add these test strings if they weren’t already in there).[5] Modify the program from the previous exercise so that the word ending with the letter
ais captured into memory$1. Update the code to display that variable’s contents in single quotes, something like$1 contains 'Wilma'.[5] Modify the program from the previous exercise to use named captures instead of relying on
$1. Update the code to display that label name, something like'word' contains 'Wilma'.[5] Extra credit exercise: modify the program from the previous exercise ...