use strict;
my $str = '<p> </p>
<p> </p>
https://www.youtube.com/watch?v=yCrjqNJgkNA
<p> </p>
<p> </p>
www.youtube.com/watch?v=TkRYM3Vt5v4
<p> </p>
<p> </p>
<p> </p>
http://www.youtube.com/watch?v=nfPYWD3Hxd4
<p><a href="https://www.youtube.com/watch?v=xV-ny2fNauA">https://www.youtube.com/watch?v=xV-ny2fNauA</a></p>
<p><a href="https://www.youtube.com/watch?v=4Quu-22CqHg">https://www.youtube.com/watch?v=4Quu-22CqHg</a></p>
<p><a href="https://www.youtube.com/watch?v=5BlLmJxNV0s">https://www.youtube.com/watch?v=5BlLmJxNV0s</a></p>
<p><a href="https://www.youtube.com/watch?v=wtMoYjUIpDk">https://www.youtube.com/watch?v=wtMoYjUIpDk</a></p>
<p><a href="https://www.youtube.com/watch?v=9U35vNlwj3o">https://www.youtube.com/watch?v=9U35vNlwj3o</a></p>
<p><a href="https://www.youtube.com/watch?v=lFITZBBz_lk">https://www.youtube.com/watch?v=lFITZBBz_lk</a></p>
<p><a href="https://www.youtube.com/watch?v=jjIp1qSkfgg">https://www.youtube.com/watch?v=jjIp1qSkfgg</a></p>
<p><a href="https://www.youtube.com/watch?v=KM5g6wpY8a4">https://www.youtube.com/watch?v=KM5g6wpY8a4</a></p>';
my $regex = qr/(<a.*?(?:href=")?)?(https?:\/\/)?(?:www\.)?(?:youtu\.be\/|youtube\.com(?:\/embed\/|\/v\/|\/watch?.*?v=))([\w\-]{10,12})(.*<\/a>?)?/xp;
if ( $str =~ /$regex/g ) {
print "Whole match is ${^MATCH} and its start/end positions can be obtained via \$-[0] and \$+[0]\n";
# print "Capture Group 1 is $1 and its start/end positions can be obtained via \$-[1] and \$+[1]\n";
# print "Capture Group 2 is $2 ... and so on\n";
}
# ${^POSTMATCH} and ${^PREMATCH} are also available with the use of '/p'
# Named capture groups can be called via $+{name}
Please keep in mind that these code samples are automatically generated and are not guaranteed to work. If you find any syntax errors, feel free to submit a bug report. For a full regex reference for Perl, please visit: http://perldoc.perl.org/perlre.html