Created
November 28, 2020 13:22
-
-
Save erickythierry/737f3a1ed980455036cde57a16919329 to your computer and use it in GitHub Desktop.
regex youtube python
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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