4.5.1 The Caret Anchor
4.5.1 The Caret Anchor
If we need to specify a specific location inside the string for the pattern to occur, we need to use what are called anchors. In this specific case, if we want to capture only the SMTP headers for complete messages and not for enclosed or forwarded messages, we want the string From: to start in the beginning of the string or the line with no preceding spaces. We can make Perl look for this condition if we anchor the pattern at the beginning of the string. In Perl, we do so by putting a caret (^) in the beginning of the pattern. Therefore, the following is an update
to the previous program on finding who has sent mail.
Program 4.15
#!/usr/bin/perl
while (<>){
if (/^From:/ or /^Subject:/ or /^Date:/){
print $_;
}
}
The output of this program looks like the following.
From: "Mike Millner"Subject: Re: vedanti.com and shillong.com Date: Fri, 24 Jul 1998 14:32:22 -0600 From: hostmaster@internic.net Subject: Re: [NIC-980724.18471] NEW DOMAIN indiashipping.com (fwd) Date: Fri, 24 Jul 1998 13:27:59 -0400 (EDT) From: "Brahma, Malavika (Exchange)" Subject: RE: FW: Exe. Comm Date: Fri, 7 Aug 1998 14:08:50 -0400
Note that no MIME headers from forwarded or enclosed messages are included. This is because we have included the caret in each of the three patterns that we have in the program. We also note that the Delivery-Date: MIME header is not included any more. This is because such a MIME header does not have Date starting at column zero.
The caret (^) is only one of several anchors that Perl provides. Another anchor is $ that anchors a pattern to the very end of a string. Two other useful anchors are \b that stands for a word boundary and \B that stands for “not a word boundary.” We will see uses of these in some examples below.
