小骆驼图书第八章练习
1.
my $str = "beforematchafter";
if($str =~ /match/){ #注意$`--before the match; $& --match thing ;$' --after the match;
print "before match is $` \n";
print "match is $& \n";
print "aftre the match is $' \n";
}
```
2.
foreach(<>){
if(/\w*a\s/){ //匹配以a为结尾,后接空格(/s);
print “find the word end by a $_”;
}
}
3.
foreach(<>){
chomp;
if(/(\w*a)\s/){
print “find the word end by a in $_, and the word is $1 \n”;
}
}
4.
foreach(<>){
chomp;
if(/(\w*a)(.{0,5})/){ #注意使用括号将匹配内容存放进$1,$2等变量;
print “find the word end by a in $_, and the word is $1 \n”;
print “the word after is $2\n”;
}
else {
print “no match \n”;
}
}
5.
foreach(<>){
chomp;
if(/\s$/){
print “use the # for the end of line $_# \n”;
}
else {
print “no match \n”;
}
}