匹配多行文本 /m用法

来源:互联网 发布:vb.net 教程 编辑:程序博客网 时间:2024/04/30 01:55

1
00:00:09,855 --> 00:00:14,724
Britain was an object of desire.

2
00:00:26,295 --> 00:00:31,847
Tacitus declared it worth the conquest,

3
00:00:32,015 --> 00:00:35,803
the best compliment
that could occur to a Roman.
如果以句号为分割,把对应的时间合并起来,最后得到下面的结果:
1
00:00:09,855 --> 00:00:14,724
Britain was an object of desire.
2
00:00:26,295 --> 00:00:35,803
Tacitus declared it worth the conquest,
the best compliment
that could occur to a Roman.

————————————————————————————————————————————————

 

#!/usr/bin/perl
#ans.pl
my $file;
while (<>){s/^/s+$//;$file.=$_};
my @data=split//./,$file;

for (@data){
    /.*(^/d.*>.*/d$)/sm;  #由于@data中是按分段文本存放,所以需要用到多行匹配,/s表示匹配/n,/m多行匹配

    my $time=$1;
    s/^/d+:.*/d$//gm;     #删除时间行
    s/^/d+/n//gm;           #删除数目行
    s/$/./;                        #加上点号
    s//n+//n/g;                 #删除空行
    print ++$num,"/n",$time,$_,"/n";
}

=====================
perl  ans.pl  yourfile.txt

原创粉丝点击