perlRegex,檔案讀取常用方法

來源:互聯網
上載者:User
好久沒有寫perl指令碼了,最近電腦上面有《prison break》的音頻,也有srt的歌詞,不過歌詞不對,最後進行下面的指令碼處理後,可以變聽變看字幕了。主要是用來複習下perl指令碼,Regex,檔案讀取,迴圈,條件等#! /usr/bin/perl #print 'done';# my $file = '<E:\Prison Break1\Prison Break - 1x02 - Allen [eng-subtitle].srt';# my $out  = '>E:\Prison Break1\P02.srt';# open(FILE, $file) or die "$!\n";# open(RESULT, $out) or die $!;my $dirMp3AndLrc = 'E:\Prison Break1';#MP3,lrc檔案都在此檔案夾中,my $dirLrcTarget = 'E:\PB';opendir(MP3_AND_LRC, $dirMp3AndLrc) or die $!;my @mp3;#mp3格式my @lrc;#lrc格式my $hashMp3;my $hashLrc;while($file = readdir(MP3_AND_LRC)) {# $i = 0;if ($file =~ /\.srt$/) {# push @lrc, $file;# print $file . "\n";my $lyric = "<$dirMp3AndLrc\\$file";open(SRT, $lyric) or die "$!\n";open(RESULT, ">$dirLrcTarget\\$file") or die "$!\n";# print ">$dirMp3\\$file\n";my $li;my @final;my @lines = <SRT>;for(my $i=0; $i<@lines; $i++) {$line = $lines[$i];# # print 'before: i=' . $i . '; v=' . $line;#. "\n";# $li .= $li2;# # print 'after: i=' . $i . '; v=' . $line;#. "\n";# $line =~ s/[\r\n]//;if ($line =~ /(^\d)/ or $line =~ /(^$)/s){# @final[] = $line;# print "tag\n";push @final, $line;} else {# print "real=\n";my $realWords = '';my $tmp;for ($tmp = 0; $tmp<@lines; $tmp++) {# print 't=' . $tmp . "\n";# print 'v=' .$lines[$i+$tmp] . "\n";if ($lines[$i+$tmp] =~ /(^\d)/ or $lines[$i+$tmp] =~ /(^$)/s){last;} else {$realWords .= $lines[$i+$tmp];}}$i += $tmp;$realWords =~ s/\n/ /g;push @final, $realWords . "\n\n";print 'realWords=' . $realWords . "\n";}}# print 'line=' . $li;print RESULT @final;close(SRT);close(RESULT);}}# foreach(@lrc) {# $name = $_;# $list = substr($name, 17, 2);# $list =~ s/^0//;# $name =~ s/\.srt$//;# rename "$dir\\$list.mp3", "$dir\\$name.mp3";  # print "before modify: $dir\\$list.mp3, \n\nafter modify: $dir\\$name.mp3\n";# } closedir PB;exit;

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.