第八章:以正则表达式进行匹配
上一篇 /
下一篇 2008-11-12 11:30:03
/ 个人分类:《Perl语言入门》-小骆驼书-练习题答案
1)
use warnings;
while(<>){
chomp;
if (/match/){
print "Matched: |$`<$&>$'|\n"; # it's better to replace $& to match.
} else {
print "No matched: |$_|\n";
}
}
2)
use warnings;
while(<>){
chomp;
if (/\w+a(\s+|$)/){ #/a\b/
print "Matched: |$`<$&>$'|\n";
} else {
print "No matched: |$_|\n";
}
}
3)
use warnings;
while(<>){
chomp;
if (/(\w+a\s+|\w+a$)/){ #/(\b\w*a\b)/
print "Matched: '$1'\n";
} else {
print "No matched: |$_|\n";
}
}
4)
use warnings;
while(<>){
chomp;
if (/(\w+a\b)/){ #\w+a\s+|\w+a$
print "Matched: $`'$&'$'\n";
if($' =~ /(\w|\s|\W){1,5}/){
print "$&\n";
}
} else {
print "No matched: |$_|\n";
}
}
5)
use warnings;
while(<>){
chomp;
if (/\s+$/){
print "$_|\n";
}
}
收藏
举报
TAG: