Skip to content

Instantly share code, notes, and snippets.

@erickythierry
Created November 28, 2020 13:22
Show Gist options
  • Save erickythierry/737f3a1ed980455036cde57a16919329 to your computer and use it in GitHub Desktop.
Save erickythierry/737f3a1ed980455036cde57a16919329 to your computer and use it in GitHub Desktop.
regex youtube python
import re
def youtube_url_validation(url):
youtube_regex = (
r'(https?://)?(www\.)?'
'(youtube|youtu|youtube-nocookie)\.(com|be)/'
'(watch\?v=|embed/|v/|.+\?v=)?([^&=%\?]{11})')
youtube_regex_match = re.match(youtube_regex, url)
if youtube_regex_match:
return youtube_regex_match.group(6)
return youtube_regex_match
youtube_urls_test = [
'http://www.youtube.com/watch?v=5Y6HSHwhVlY',
' http://youtu.be/5Y6HSHwhVlY'
]
for url in youtube_urls_test:
m = youtube_url_validation(url)
if m:
print(m)
else:
print(m)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment