Free Trial

Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.


  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • DownloadDownload
  • PrintPrint
Share this Page URL
Help

A. Exercise Answers > Answers to Chapter 7 Exercises

Answers to Chapter 7 Exercises

  1. Here’s one way to do it:

    while (<>) {
      if (/fred/) {
        print;
      }
    }

    This is pretty simple. The more important part of this exercise is trying it out on the sample strings. It doesn’t match Fred, showing that regular expressions are case-sensitive. (We’ll see how to change that later.) It does match frederick and Alfred, since both of those strings contain the four-letter string fred. (Matching whole words only, so that frederick and Alfred wouldn’t match, is another feature we’ll see later.)

  2. Here’s one way to do it: change the pattern used in the first exercise’s answer to /[fF]red/. You could also have tried /(f|F)red/ or /fred|Fred/, but the character class is more efficient.


  

You are currently reading a PREVIEW of this book.

                                                                                        

Get instant access to over
$1 million worth of books and videos.

  

Start a Free Trial