I need to search for words in a txt file (see below) that begin with "p" and "P" and words that end with "p" and "P" and print them out. Thank the people who have helped me answer my previous question.
Text File:
CPAN stands for comprehensive Perl Archive Network.
^ and $ are used as anchors in a regular expression.
/pattern/ is a pattern match operator.
Perl is very easy to learn.
Enter ‘H’ or ‘h’ for help.
#!/usr/local/bin/perl
open INPUT, "<proj2_part3.txt" or die "File not found\n";
while (<INPUT>)
{
my($line) = $_;
chomp($line);
if ($line =~ m\b\^p*\bi) {print "$&\n";}
if ($line =~ m/p$*/i) {print "$&\n";}
else {print "No match, sorry.\n";}
}
close INPUT;
The output is supposed to be:
Perl
pattern
pattern
Perl
help
But I only get:
P
p
p
P
p
How do I make the program print out the whole word?












