Skip to content

Instantly share code, notes, and snippets.

@stevedya
Created March 15, 2019 15:45
Show Gist options
  • Save stevedya/ccd8c614cebda0b8c29ff33321b0a871 to your computer and use it in GitHub Desktop.
Save stevedya/ccd8c614cebda0b8c29ff33321b0a871 to your computer and use it in GitHub Desktop.
Using regex this gist will return video ID's from strings
import re
# Vimeo
vimeo_url = 'https://vimeo.com/56282283'
# Regex to strip id from video string
match_pattern = r'^(?:https?:)?(?:\/\/)?(?:www\.|player\.)?vimeo.com\/(?:channels\/(?:\w+\/)?|groups\/([^\/]*)\/videos\/|album\/(\d+)\/video\/|video\/|)(\d+)(?:$|\/|\?)'
response = re.search(match_pattern, url).group(3)
if response:
print (response)
else:
print (url)
# Youtube
youtube_url = 'https://www.youtube.com/watch?v=6D-A6CL3Pv8'
# Regex to strip id from video string
pattern = r'(?:[?&]v=|\/embed\/|\/1\/|\/v\/|https:\/\/(?:www\.)?youtu\.be\/)([^&\n?#]+)'
youtube_response = re.search(pattern, youtube_url).group(1)
if youtube_response:
print(youtube_response)
else:
print(youtube_url)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment