jacksp has asked for the wisdom of the Perl Monks concerning the following question:

Hi, I have 2 regex with me which help me in embedding youtube video. The 1st regex attaches [video] ... [/video] BBcode around youtube URL and 2nd regex does the actual embedding of youtube code. I'm looking for one regex which does both of these.
#add video tag to video urls inserted without video tag $text =~ s#(^|\s|\>)((http|https)://www.youtube.com/watch\?\.*v=([a-z0 +-9-_%]+)[&\w;=\+\-]*)#$1\[video\]$2\[\/video\]#isg; #Embed respective video code $text =~ s#\[video\](http|https)://www.youtube.com/watch\?\.*v +=([a-z0-9-_%]+)[&\w;=\+\-]*\[\/video\]#<object class="restrain" type= +"application\/x-shockwave-flash" width="640" height="385" data="http: +\/\/www\.youtube\.com\/v\/$2"><param name="movie" value="http://www.y +outube.com/v/$2"/><param name="wmode" value="transparent"/><embed width="640" height="385" type="app +lication/x-shockwave-flash" src="http://www.youtube.com/v/$2" /></obj +ect>#isg;
The sample text may contain video with/without video BB code, so I need to satisfy both conditions and embed in both the cases. Eg: $text= " Video1: http://www.youtube.com/watch?v=dTi4v3HveqE Video2: [video]http://www.youtube.com/watch?v=dTi4v3HveqE[/video]"; Any help on how to merge the 2 regex ?

Replies are listed 'Best First'.
Re: Youtube video parser
by Anonymous Monk on Jun 13, 2012 at 07:33 UTC

    Don't :) I would use Parse::BBCode instead, with a custom tag definition for video, and a custom text_processor / url_finder callback , see example in Parse-BBCode-0.14/t/14_info.t

    $text =~ s{ (?: (^|\s|\>) # $1 ( # $2 (http|https) # $3 ://www.youtube.com/watch\?\.*v= ([a-z0-9-_%]+) # $4 [&\w;=\+\-]* ) ) | (?: \Q[video]\E (^|\s|\>) # $5 ( # $6 (http|https) # $7 ://www.youtube.com/watch\?\.*v= ([a-z0-9-_%]+) # $8 [&\w;=\+\-]* ) \Q[/video]\E ) }{ my $ret = ""; my $https = $3 || $7; my $vid = $4 || $8; $ret .= "<embed>"; $ret .= escapeHTML($vid); $ret .= "</embed>"; $ret; }gmsxie;
      And tested :)
      #!/usr/bin/perl -- use strict; use warnings; use Data::Dump qw/ dd /; use CGI qw/ escapeHTML /; my $text = "Video1: http://www.youtube.com/watch?v=dTi4v3HveqE Video2: [video]http://www.youtube.com/watch?v=dTi4v3HveqE[/video]"; $text =~ s{ (?: (^|\s|\>) # $1 ( # $2 (http|https) # $3 ://www.youtube.com/watch\?\S*v= ([a-z0-9-_%]+) # $4 [&\w;=\+\-]* ) ) | (?: \Q[video]\E ( # $5 (http|https) # $6 ://www.youtube.com/watch\?\S*v= ([a-z0-9-_%]+) # $7 [&\w;=\+\-]* ) \Q[/video]\E ) }{ my $ret = ""; my $https = $3 || $6; my $vid = $4 || $7; $ret .= "\n<embed>\n"; $ret .= escapeHTML($vid); $ret .= "\n</embed>\n"; $ret; }gmsxie; dd $text; print "\n\n$text\n"; __END__ "Video1:\n<embed>\ndTi4v3HveqE\n</embed>\n\nVideo2: \n<embed>\ndTi4v3H +veqE\n</embed>\n" Video1: <embed> dTi4v3HveqE </embed> Video2: <embed> dTi4v3HveqE </embed>