# coding=utf8
# the above tag defines encoding for this document and is for Python 2.x compatibility
import re
regex = r"(?:[?&]vi?=|\/embed\/|\/\d\d?\/|\/vi?\/|https?:\/\/(?:www\.)?youtu\.be\/)([A-Za-z0-9_\-]{11})"
test_str = ("https://www.youtube.com/watch?v=0zM3nApSvMg&feature=feedrec_grec_index\n"
"https://www.youtube.com/user/IngridMichaelsonVEVO#p/a/u/1/QdK8U-VIH_o\n"
"https://www.youtube.com/v/0zM3nApSvMg?fs=1&hl=en_US&rel=0\n"
"https://www.youtube.com/watch?v=0zM3nApSvMg#t=0m10s\n"
"https://www.youtube.com/embed/0zM3nApSvMg?rel=0\n"
"https://www.youtube.com/watch?v=KdwsulMb8EQ\n"
"https://youtu.be/0zM3nApSvMg\n"
"//www.youtube-nocookie.com/embed/up_lNV-yoK4?rel=0\n"
"http://www.youtube.com/user/Scobleizer#p/u/1/1p3vcRhsYGo\n"
"http://www.youtube.com/watch?v=cKZDdG9FTKY&feature=channel\n"
"http://www.youtube.com/watch?v=yZ-K7nCVnBI&playnext_from=TL&videos=osPknwzXEas&feature=sub\n"
"http://www.youtube.com/ytscreeningroom?v=NRHVzbJVx8I\n"
"http://www.youtube.com/watch?v=6dwqZw0j_jY&feature=youtu.be\n"
"http://www.youtube.com/user/Scobleizer#p/u/1/1p3vcRhsYGo?rel=0\n"
"http://www.youtube.com/watch?v=cKZDdG9FTKY&feature=channel\n"
"http://www.youtube.com/watch?v=yZ-K7nCVnBI&playnext_from=TL&videos=osPknwzXEas&feature=sub\n"
"http://www.youtube.com/ytscreeningroom?v=NRHVzbJVx8I\n"
"http://www.youtube.com/embed/nas1rJpm7wY?rel=0\n"
"http://www.youtube.com/watch?v=peFZbP64dsU\n"
"http://youtube.com/v/dQw4w9WgXcQ?feature=youtube_gdata_player\n"
"http://youtube.com/?v=dQw4w9WgXcQ&feature=youtube_gdata_player\n"
"http://www.youtube.com/watch?v=dQw4w9WgXcQ&feature=youtube_gdata_player\n"
"http://youtube.com/?vi=dQw4w9WgXcQ&feature=youtube_gdata_player\n"
"http://youtube.com/watch?v=dQw4w9WgXcQ&feature=youtube_gdata_player\n"
"http://youtube.com/watch?vi=dQw4w9WgXcQ&feature=youtube_gdata_player\n"
"http://youtube.com/vi/dQw4w9WgXcQ?feature=youtube_gdata_player\n"
"http://youtu.be/dQw4w9WgXcQ?feature=youtube_gdata_player\n"
"http://youtu.be/afa-5HQHiAs\n"
"http://youtu.be/6dwqZw0j_jY\n"
"http://www.youtube.com/user/SilkRoadTheatre#p/a/u/2/6dwqZw0j_jY\n"
"https://www.youtube.com/watch?v=ishbTyLs6ps&list=PLGup6kBfcU7Le5laEaCLgTKtlDcxMqGxZ&index=106&shuffle=2655\n"
"http://www.youtube.com/v/0zM3nApSvMg?fs=1&hl=en_US&rel=0\n"
"http://www.youtube.com/embed/r1nq87PzecI?rel=0\n"
"http://www.youtube.com/watch?v=0zM3nApSvMg&feature=feedrec_grec_index\n"
"http://www.youtube.com/watch?v=r1nq87PzecI\n"
"http://youtu.be/0zM3nApSvMg\n"
"http://www.youtube.com/watch?v=0zM3nApSvMg#t=0m10s\n"
"http://www.youtube.com/user/IngridMichaelsonVEVO#p/a/u/1/KdwsulMb8EQ\n"
"http://youtu.be/dQw4w9WgXcQ\n"
"http://www.youtube.com/embed/dQw4w9WgXcQ\n"
"https://www.youtube.com/watch?v=r1nq87PzecI\n"
"http://www.youtube.com/v/dQw4w9WgXcQ\n"
"http://www.youtube.com/e/dQw4w9WgXcQ\n"
"http://www.youtube.com/watch?v=dQw4w9WgXcQ\n"
"http://www.youtube.com/?v=dQw4w9WgXcQ\n"
"http://www.youtube.com/watch?feature=player_embedded&v=dQw4w9WgXcQ\n"
"http://www.youtube.com/?feature=player_embedded&v=dQw4w9WgXcQ\n"
"http://www.youtube.com/user/IngridMichaelsonVEVO#p/u/11/KdwsulMb8EQ\n"
"http://www.youtube-nocookie.com/v/6L3ZvIMwZFM?version=3&hl=en_US&rel=0\n"
"http://www.youtube.com/user/dreamtheater#p/u/1/oTJRivZTMLs\n"
"https://youtu.be/oTJRivZTMLs?list=PLToa5JuFMsXTNkrLJbRlB--76IAOjRM9b\n"
"http://www.youtube.com/watch?v=oTJRivZTMLs&feature=youtu.be\n"
"https://youtu.be/oTJRivZTMLs\n"
"http://youtu.be/r1nq87PzecI&feature=channel\n"
"http://www.youtube.com/ytscreeningroom?v=oTJRivZTMLs\n"
"http://www.youtube.com/embed/oTJRivZTMLs?rel=0\n"
"http://youtube.com/v/oTJRivZTMLs&feature=channel\n"
"http://youtube.com/v/r1nq87PzecI&feature=channel\n"
"http://youtube.com/vi/KdwsulMb8EQ&feature=channel\n"
"http://youtube.com/?v=r1nq87PzecI&feature=channel\n"
"http://youtube.com/?feature=channel&v=r1nq87PzecI?rel=0\n"
"http://youtube.com/?vi=oTJRivZTMLs&feature=channel\n"
"http://youtube.com/watch?v=oTJRivZTMLs&feature=channel\n"
"http://youtube.com/watch?vi=KdwsulMb8EQ&feature=channel")
matches = re.finditer(regex, test_str)
for matchNum, match in enumerate(matches, start=1):
print ("Match {matchNum} was found at {start}-{end}: {match}".format(matchNum = matchNum, start = match.start(), end = match.end(), match = match.group()))
for groupNum in range(0, len(match.groups())):
groupNum = groupNum + 1
print ("Group {groupNum} found at {start}-{end}: {group}".format(groupNum = groupNum, start = match.start(groupNum), end = match.end(groupNum), group = match.group(groupNum)))
# Note: for Python 2.7 compatibility, use ur"" to prefix the regex and u"" to prefix the test string and substitution.
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 Python, please visit: https://docs.python.org/3/library/re.html