Perl语言入门-第九章-用正则表达式处理文本-习题

1. 题目

   

   

2. 代码与输出   

 1  # -----------------------------------------------------------#
 2  # Source: Learning Perl, chapter9,exercise-1
 3  # Date:   2012-01-19
 4  # Author: xiaodongrush
 5  #-----------------------------------------------------------#
 6  use  5.010;
 7  @array = qw {
 8   fredfredfred
 9   fredfredbarney
10   barneyfredfred
11   barneybarneybarney
12   fred|barney
13 };
14  @content = qw {
15   fred fred|barney
16 };
17  foreach  $what( @content) {
18   say  " \$what = $what ";
19   say  " \@array = (@array) ";
20    foreach  $text( @array) {
21      if( $text =~ m/(?: $what){ 3}/) {
22       say  " <$`><$&><$'> ";
23     }
24   }
25   say  " ---------------------------- ";
26 }
27 <STDIN>;
28  # -----------------------------------------------------------#
29  # 将某个字符串重复3次,注意这个字符串可能包含或|这样的字符,因此将其用括号
30  # 括起来就好了
31  #-----------------------------------------------------------#

 1  # -----------------------------------------------------------#
 2  # Source: Learning Perl, chapter9,exercise-2
 3  # Date:   2012-01-19
 4  # Author: xiaodongrush
 5  #-----------------------------------------------------------#
 6  use  5.010;
 7  $input_file =  $ARGV[ 0];
 8  $output_file =  $ARGV[ 0] .  " .out ";
 9  unless(! open IN,  " <$input_file ") {
10   say  " Can't read <$input_file> ! ";
11 }
12  unless( open OUT,  " >$output_file ") {
13   say  " Can't write <$output_file> ! ";
14 }
15  while(<IN>) {
16   s/Fred/Larry/gi;
17    print OUT  $_;
18 }
19  # -----------------------------------------------------------#
20  # 只读<,只写>,追加写>>,默认就是读写
21  #-----------------------------------------------------------#

 1  # -----------------------------------------------------------#
 2  # Source: Learning Perl, chapter9,exercise-3
 3  # Date:   2012-01-19
 4  # Author: xiaodongrush
 5  #-----------------------------------------------------------#
 6  use  5.010;
 7  $text =  " fred&wilma&fred&wilma&fred&wima ";
 8 say  " text = $text ";
 9  @array =  split /fred/gi,  $text;
10  foreach( @array) {
11   s/wilma/Fred/gi;
12 }
13  $text =  join Wilma,  @array;
14 say  " text = $text ";
15 <STDIN>;
16  # -----------------------------------------------------------#
17  #1.该解法是用Fred作为分割符,将字符串分开,然后,将分开的
18  #  字符串中的wilma环卫Fred,然后再用Wilma将分开的字符串连起来
19  #2.书中解法是,首先对字符串chomp,保证不含\n,然后将Fred都换为
20  #  \n,接着讲Wilma都换为Fred,最后将\n都换为Wilma
21  #-----------------------------------------------------------#

 

 1  # -----------------------------------------------------------#
 2  # Source: Learning Perl, chapter9,exercise-4
 3  # Date:   2012-01-19
 4  # Author: xiaodongrush
 5  #-----------------------------------------------------------#
 6  use  5.010;
 7 $^I =  " .bak ";
 8  $text =  " ## Copyright (C) 2012 by Yours Truly\n ";
 9  while(<>) {
10    if(/ # !/) {
11       $_ =  $_ .  $text;
12   }
13    print  $_;
14 }
15  # -----------------------------------------------------------#
16  # 1. /i表示忽略大小写,/g表示全局匹配
17  # 2. $^I,在读入文件假设是word.txt,首先将其改名为word.txt.out,
18  #    然后创建一个空的word.txt文件,接着,word.txt.out的每一行,
19  #    并且通过print函数写入到word.txt文件中。
20  #-----------------------------------------------------------#

 1  # -----------------------------------------------------------#
 2  # Source: Learning Perl, chapter9,exercise-4
 3  # Date:   2012-01-19
 4  # Author: xiaodongrush
 5  #-----------------------------------------------------------#
 6  use  5.010;
 7  $text =  " ## Copyright (C) 2012 by Yours Truly\n ";
 8  foreach( @ARGV) {
 9    $files{ $_} =  1;
10 }
11  while(<>) {
12    if(/ # # Copyright/) {
13       delete  $files{ $ARGV};
14   }
15 }
16  @ARGV =  keys  %files;
17 $^I =  " .bak ";
18  if( @ARGV !=  0) {
19    while(<>) {
20      if(/ # !/) {
21         $_ =  $_ .  $text;
22     }
23      print  $_;
24   }
25 }
26  # -----------------------------------------------------------#
27  # 1. /i表示忽略大小写,/g表示全局匹配
28  # 2. $^I,在读入文件假设是word.txt,首先将其改名为word.txt.out,
29  #    然后创建一个空的word.txt文件,接着,word.txt.out的每一行,
30  #    并且通过print函数写入到word.txt文件中。
31  #-----------------------------------------------------------#

输出略,与前一题类似。

3. 文件

    /Files/pangxiaodong/LearningPerl/ch9-answer.rar

转载于:https://www.cnblogs.com/pangxiaodong/archive/2012/01/19/2326353.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值