Section 7.4. Exercises


7.4. Exercises

See Appendix A 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 the exercises in this chapter are more important than the others. Expect the unexpected.

  1. [10] Make a program that prints each line of its input that mentions fred. (It shouldn't do anything for other lines of input.) Does it match if your input string is Fred, frederick, or Alfred? Make a small text file with a few lines mentioning "fred flintstone" and his friends. Then use that file as input to this program and the ones later in this section.

  2. [6] Modify the previous program to allow Fred to match as well. Does it match now if your input string is Fred, frederick, or Alfred? (Add lines with these names to the text file.)

  3. [6] Make a program that prints each line of its input that contains a period (.), ignoring other lines of input. Try it on the small text file from the previous exercise: Does it notice Mr. Slate?

  4. [8] Make a program that prints each line with a word that is capitalized but not ALL capitalized. Does it match Fred but neither fred nor FRED?

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



Learning Perl
Learning Perl, 5th Edition
ISBN: 0596520107
EAN: 2147483647
Year: 2003
Pages: 232

Similar book on Amazon

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net