Perl regular expressions, common methods for reading files

Source: Internet
Author: User
Tags perl script
I haven't written a Perl script for a long time. Recently, my computer has the prison break audio and SRT lyrics. However, the lyrics are incorrect. After the script is processed, you can change your voice to subtitles. It is mainly used to review Perl scripts, regular expressions, file reading, loops, conditions, etc #! /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 and LRC files are all in this folder. My $ dirlrctarget = 'e: \ Pb'; opendir (mp3_and_lrc, $ dirmp3andlrc) or die $ !; My @ MP3; # MP3 format my @ LRC; # LRC format 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"; 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 \ $ listparts", "$ dir \ $ name1_"; # print" before modify: $ dir \ $ listparts, \ n \ nafter modify: $ dir \ $ name1_\ N ";#} closedir Pb; exit;

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.