# coding=utf8
# the above tag defines encoding for this document and is for Python 2.x compatibility
import re
regex = r"(?<=watch\?v=|\/videos\/|embed\/|youtu.be\/|\/v\/|watch\?v%3D|%2Fvideos%2F|embed%2F|youtu.be%2F|%2Fv%2F)[^#\&\?\n]*"
test_str = ("thisdoesnotmatch\n"
"seulement11\n"
"l_la5XiQJdk\n\n"
"http://www.youtube.com/watch?v=vpiMAaPTze8\n"
"http://youtu.be/l_la5XiQJdk\n"
"http://youtu.be/NLqAF9hrVbY\n"
"https://youtu.be/qT47KF5pvfw\n"
"https://youtu.be/zImHyTyYhM8?t=4s\n"
"http://www.youtube.com/v/NLqAF9hrVbY?fs=1&hl=en_US\n"
"http://www.youtube.com/v/NLqAF9hrVbY?fs=1&hl=en_US\n"
"http://www.youtube.com/watch?v=NLqAF9hrVbY\n"
"http://www.youtube.com/user/Scobleizer#p/u/1/1p3vcRhsYGo\n"
"http://www.youtube.com/ytscreeningroom?v=NRHVzbJVx8I\n"
"http://www.youtube.com/sandalsResorts#p/c/54B8C800269D7C1B/2/PPS-8DMrAn4\n"
"http://gdata.youtube.com/feeds/api/videos/NLqAF9hrVbY\n"
"http://www.youtube.com/watch?v=spDj54kf-vY&feature=g-vrec\n"
"http://www.youtube.com/watch?v=spDj54kf-vY&feature=youtu.be\n"
"http://www.youtube-nocookie.com/watch?v=NLqAF9hrVbY\n"
"http://www.youtube.com/embed/NLqAF9hrVbY\n"
"https://www.youtube.com/embed/NLqAF9hrVbY\n"
"https://www.youtube.com/watch?v=MRl7cxSOXdU&feature=youtu.be\n"
"https://www.youtube.com/watch?v=q07SQFmL4rM\n"
"https://www.youtube.com/watch?v=q07SQFmL4yM\n"
"https://www.youtube.com/watch?time_continue=4&v=zImHyTyYhM8\n"
"http://www.youtube.com/embed/dQw4w9WgXcQ ...\n"
"http://www.youtube.com/watch?v=dQw4w9WgXcQ ...\n"
"http://www.youtube.com/?v=dQw4w9WgXcQ ...\n"
"http://www.youtube.com/v/dQw4w9WgXcQ ...\n"
"http://www.youtube.com/e/dQw4w9WgXcQ ...\n"
"http://www.youtube.com/user/username#p/u/11/dQw4w9WgXcQ ...\n"
"http://www.youtube.com/sandalsResorts#p/c/54B8C800269D7C1B/0/dQw4w9WgXcQ ...\n"
"http://www.youtube.com/watch?feature=player_embedded&v=dQw4w9WgXcQ ...\n"
"http://www.youtube.com/?feature=player_embedded&v=dQw4w9WgXcQ ...\n"
"https://www.youtube.com/watch?v=DFYRQ_zQ-gk&feature=featured\n"
"https://www.youtube.com/watch?v=DFYRQ_zQ-gk\n"
"http://www.youtube.com/watch?v=DFYRQ_zQ-gk\n"
"//www.youtube.com/watch?v=DFYRQ_zQ-gk\n"
"www.youtube.com/watch?v=DFYRQ_zQ-gk\n"
"https://youtube.com/watch?v=DFYRQ_zQ-gk\n"
"http://youtube.com/watch?v=DFYRQ_zQ-gk\n"
"//youtube.com/watch?v=DFYRQ_zQ-gk\n"
"youtube.com/watch?v=DFYRQ_zQ-gk\n"
"https://m.youtube.com/watch?v=DFYRQ_zQ-gk\n"
"http://m.youtube.com/watch?v=DFYRQ_zQ-gk\n"
"//m.youtube.com/watch?v=DFYRQ_zQ-gk\n"
"m.youtube.com/watch?v=DFYRQ_zQ-gk\n"
"https://www.youtube.com/v/DFYRQ_zQ-gk?fs=1&hl=en_US\n"
"http://www.youtube.com/v/DFYRQ_zQ-gk?fs=1&hl=en_US\n"
"//www.youtube.com/v/DFYRQ_zQ-gk?fs=1&hl=en_US\n"
"www.youtube.com/v/DFYRQ_zQ-gk?fs=1&hl=en_US\n"
"youtube.com/v/DFYRQ_zQ-gk?fs=1&hl=en_US\n"
"https://www.youtube.com/embed/DFYRQ_zQ-gk?autoplay=1\n"
"https://www.youtube.com/embed/DFYRQ_zQ-gk\n"
"http://www.youtube.com/embed/DFYRQ_zQ-gk\n"
"//www.youtube.com/embed/DFYRQ_zQ-gk\n"
"www.youtube.com/embed/DFYRQ_zQ-gk\n"
"https://youtube.com/embed/DFYRQ_zQ-gk\n"
"http://youtube.com/embed/DFYRQ_zQ-gk\n"
"//youtube.com/embed/DFYRQ_zQ-gk\n"
"youtube.com/embed/DFYRQ_zQ-gk\n"
"https://youtu.be/DFYRQ_zQ-gk?t=120\n"
"https://youtu.be/DFYRQ_zQ-gk\n"
"http://youtu.be/DFYRQ_zQ-gk\n"
"//youtu.be/DFYRQ_zQ-gk\n"
"youtu.be/DFYRQ_zQ-gk\n"
"https://www.youtube.com/watch?v=DFYRQ_zQ-gk&feature=featured\n"
"https://www.youtube.com/watch?v=DFYRQ_zQ-gk\n"
"http://www.youtube.com/watch?v=DFYRQ_zQ-gk\n"
"//www.youtube.com/watch?v=DFYRQ_zQ-gk\n"
"www.youtube.com/watch?v=DFYRQ_zQ-gk\n"
"https://youtube.com/watch?v=DFYRQ_zQ-gk\n"
"http://youtube.com/watch?v=DFYRQ_zQ-gk\n"
"//youtube.com/watch?v=DFYRQ_zQ-gk\n"
"youtube.com/watch?v=DFYRQ_zQ-gk\n\n"
"https://m.youtube.com/watch?v=DFYRQ_zQ-gk\n"
"http://m.youtube.com/watch?v=DFYRQ_zQ-gk\n"
"//m.youtube.com/watch?v=DFYRQ_zQ-gk\n"
"m.youtube.com/watch?v=DFYRQ_zQ-gk\n\n"
"https://www.youtube.com/v/DFYRQ_zQ-gk?fs=1&hl=en_US\n"
"http://www.youtube.com/v/DFYRQ_zQ-gk?fs=1&hl=en_US\n"
"//www.youtube.com/v/DFYRQ_zQ-gk?fs=1&hl=en_US\n"
"www.youtube.com/v/DFYRQ_zQ-gk?fs=1&hl=en_US\n"
"youtube.com/v/DFYRQ_zQ-gk?fs=1&hl=en_US\n\n"
"https://www.youtube.com/embed/DFYRQ_zQ-gk?autoplay=1\n"
"https://www.youtube.com/embed/DFYRQ_zQ-gk\n"
"http://www.youtube.com/embed/DFYRQ_zQ-gk\n"
"//www.youtube.com/embed/DFYRQ_zQ-gk\n"
"www.youtube.com/embed/DFYRQ_zQ-gk\n"
"https://youtube.com/embed/DFYRQ_zQ-gk\n"
"http://youtube.com/embed/DFYRQ_zQ-gk\n"
"//youtube.com/embed/DFYRQ_zQ-gk\n"
"youtube.com/embed/DFYRQ_zQ-gk\n\n"
"https://youtu.be/DFYRQ_zQ-gk?t=120\n"
"https://youtu.be/DFYRQ_zQ-gk\n"
"http://youtu.be/DFYRQ_zQ-gk\n"
"//youtu.be/DFYRQ_zQ-gk\n"
"youtu.be/DFYRQ_zQ-gk\n\n"
"https://www.youtube.com/HamdiKickProduction?v=DFYRQ_zQ-gk\n")
matches = re.finditer(regex, test_str, re.IGNORECASE)
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