第八章:以正则表达式进行匹配

上一篇 / 下一篇  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:

 

评分:0

我来说两句

日历

« 2024-05-01  
   1234
567891011
12131415161718
19202122232425
262728293031 

数据统计

  • 访问量: 47891
  • 日志数: 80
  • 建立时间: 2008-10-27
  • 更新时间: 2009-07-17

RSS订阅

Open Toolbar