# coding=utf8
# the above tag defines encoding for this document and is for Python 2.x compatibility
import re
regex = r"(https?:\/\/)?(www.)?((player.vimeo.com\/video\/[0-9]{9})|((youtube.com|youtu\.be)\/embed\/([\w-]{9,11})))"
test_str = ("http://www.youtube.com/watch?v=GKnQGP67Xd0\n"
"https://www.youtube.com/watch?v=GKnQGP67Xd0\n"
"https://www.youtube.com/watch?v=GKnQGP67Xd0\n"
"https://www.youtube.com/watch?v=GKnQGP67Xd0\n"
"https://vimeo.com/662065137\n"
"https://vimeo.com/662065137\n"
"https://www.youtube.com/c/riotgamesmusic\n"
"https://vimeo.com/662065137\n"
"https://youtu.be/aXUCHNaZkWo\n"
"https://vimeo.com/662065137\n"
"https://vimeo.com/baconbaconbacon\n"
"https://www.youtube.com/channel/UCG_wKjFPEpdHf1Q6bTdE2JA\n"
"https://player.vimeo.com/video/662065137\n\n"
"última formato corto: http://youtu.be/NLqAF9hrVbY\n"
"iframe: http://www.youtube.com/embed/NLqAF9hrVbY\n"
"iframe (seguro): https://www.youtube.com/embed/NLqAF9hrVbY\n"
"objeto param: http://www.youtube.com/v/NLqAF9hrVbY?fs=1&hl=en_US\n"
"objeto de incrustación: http://www.youtube.com/v/NLqAF9hrVbY?fs=1&hl=en_US\n"
"ver: http://www.youtube.com/watch?v=NLqAF9hrVbY\n"
"usuarios: http://www.youtube.com/user/Scobleizer#p/u/1/1p3vcRhsYGo\n"
"ytscreeningroom: http://www.youtube.com/ytscreeningroom?v=NRHVzbJVx8I\n"
"cualquier cosa vale!: http://www.youtube.com/sandalsResorts#p/c/54B8C800269D7C1B/2/PPS-8DMrAn4\n"
"cualquier/subdominio/: http://gdata.youtube.com/feeds/api/videos/NLqAF9hrVbY\n"
"más params: http://www.youtube.com/watch?v=spDj54kf-vY&feature=g-vrec\n"
"la consulta puede tener el punto: http://www.youtube.com/watch?v=spDj54kf-vY&feature=youtu.be")
matches = re.finditer(regex, test_str, re.MULTILINE)
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