8.10. Exercises
See Appendix A for answers to the following exercises.
Several of these exercises ask you to use the test program from this chapter. You could manually type this program, taking great care to get all of the odd punctuation marks correct. But you'll probably find it faster and easier to 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.
[8] Using the pattern test program, make a pattern to match the string match. try the program with the input string beforematchafter. 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 \w sense of word) ends with the letter a. Does it match wilma but not barney? Does it match Mrs. Wilma Flintstone? What about wilma&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 the word ending with the letter a is captured into memory $1. Update the code to display that variable's contents in single quotes, something like $1 contains 'Wilma'. [5] Extra credit exercise: Modify the program from the previous exercise so that immediately following the word ending in a it will capture up to five characters (if there are that many characters) in a separate memory variable. Update the code to display both memory variables. For example, if the input string says I saw Wilma yesterday, the up to five characters are yest. If the input is I, Wilma!, the extra memory should have one character. Does your pattern still match just plain wilma? [5] Write a new program (not the test program) that prints out any input line ending with whitespace (other than a newline). Put a marker character at the end of the output line so as to make the whitespace visible.
|