in reply to Youtube video parser

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;

Replies are listed 'Best First'.
Re^2: Youtube video parser
by Anonymous Monk on Jun 13, 2012 at 07:39 UTC
    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>