Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.
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.)
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.