100:00:09,855 --> 00:00:14,724Britain was an object of desire.200:00:26,295 --> 00:00:31,847Tacitus declared it worth the conquest,300:00:32,015 --> 00:00:35,803the best complimentthat could occur to a Roman.如果以句号为分割,把对应的时
100:00:09,855 --> 00:00:14,724Britain was an object of desire.200:00:26,295 --> 00:00:31,847Tacitus declared it worth the conquest,300:00:32,015 --> 00:00:35,803the best complimentthat 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: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.
perl菜鸟,望指教
100:00:09,855 --> 00:00:14,724Britain was an object of desire.200:00:26,295 --> 00:00:31,847Tacitus declared it worth the conquest,300:00:32,015 --> 00:00:35,803the best complimentthat could occur to a Roman.如果以句号为分割,把对应的时
正则表达式(Regular Expression)方法可以满足你上面所需的要求.
Perl的正则表达式功能十分强大,基本上是常用语言中最强大的.本解使用Perl正则表达式中最基本、最常用的部分.
#!/usr/bin/perl
#ans.pl
my $file;
while (){s/^\s+$//;$file.=$_};
my @data=split/\./,$file;
for (@data){
/.*(^\d.*>.*\d$)/sm;
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
正则表达式是 Perl 语言的一大特色,也是 Perl 程序中的一点难点,不过如果能够很好的掌握他,就可以轻易地用正则表达式来完成字符串处理的任务,当然在设计中就更能得心应手了.