-
Star
(119)
You must be signed in to star a gist -
Fork
(23)
You must be signed in to fork a gist
-
-
Save rodrigoborgesdeoliveira/987683cfbfcc8d800192da1e73adc486 to your computer and use it in GitHub Desktop.
| http://www.youtube.com/watch?v=-wtIMTCHWuI | |
| http://youtube.com/watch?v=-wtIMTCHWuI | |
| http://m.youtube.com/watch?v=-wtIMTCHWuI | |
| https://www.youtube.com/watch?v=lalOy8Mbfdc | |
| https://youtube.com/watch?v=lalOy8Mbfdc | |
| https://m.youtube.com/watch?v=lalOy8Mbfdc | |
| http://www.youtube.com/watch?v=yZv2daTWRZU&feature=em-uploademail | |
| http://youtube.com/watch?v=yZv2daTWRZU&feature=em-uploademail | |
| http://m.youtube.com/watch?v=yZv2daTWRZU&feature=em-uploademail | |
| https://www.youtube.com/watch?v=yZv2daTWRZU&feature=em-uploademail | |
| https://youtube.com/watch?v=yZv2daTWRZU&feature=em-uploademail | |
| https://m.youtube.com/watch?v=yZv2daTWRZU&feature=em-uploademail | |
| http://www.youtube.com/watch?v=0zM3nApSvMg&feature=feedrec_grec_index | |
| http://youtube.com/watch?v=0zM3nApSvMg&feature=feedrec_grec_index | |
| http://m.youtube.com/watch?v=0zM3nApSvMg&feature=feedrec_grec_index | |
| https://www.youtube.com/watch?v=0zM3nApSvMg&feature=feedrec_grec_index | |
| https://youtube.com/watch?v=0zM3nApSvMg&feature=feedrec_grec_index | |
| https://m.youtube.com/watch?v=0zM3nApSvMg&feature=feedrec_grec_index | |
| http://www.youtube.com/watch?v=0zM3nApSvMg#t=0m10s | |
| http://youtube.com/watch?v=0zM3nApSvMg#t=0m10s | |
| http://m.youtube.com/watch?v=0zM3nApSvMg#t=0m10s | |
| https://www.youtube.com/watch?v=0zM3nApSvMg#t=0m10s | |
| https://youtube.com/watch?v=0zM3nApSvMg#t=0m10s | |
| https://m.youtube.com/watch?v=0zM3nApSvMg#t=0m10s | |
| http://www.youtube.com/watch?v=cKZDdG9FTKY&feature=channel | |
| http://youtube.com/watch?v=cKZDdG9FTKY&feature=channel | |
| http://m.youtube.com/watch?v=cKZDdG9FTKY&feature=channel | |
| https://www.youtube.com/watch?v=oTJRivZTMLs&feature=channel | |
| https://youtube.com/watch?v=oTJRivZTMLs&feature=channel | |
| https://m.youtube.com/watch?v=oTJRivZTMLs&feature=channel | |
| http://www.youtube.com/watch?v=lalOy8Mbfdc&playnext_from=TL&videos=osPknwzXEas&feature=sub | |
| http://youtube.com/watch?v=lalOy8Mbfdc&playnext_from=TL&videos=osPknwzXEas&feature=sub | |
| http://m.youtube.com/watch?v=lalOy8Mbfdc&playnext_from=TL&videos=osPknwzXEas&feature=sub | |
| https://www.youtube.com/watch?v=lalOy8Mbfdc&playnext_from=TL&videos=osPknwzXEas&feature=sub | |
| https://youtube.com/watch?v=lalOy8Mbfdc&playnext_from=TL&videos=osPknwzXEas&feature=sub | |
| https://m.youtube.com/watch?v=lalOy8Mbfdc&playnext_from=TL&videos=osPknwzXEas&feature=sub | |
| http://www.youtube.com/watch?v=lalOy8Mbfdc&feature=youtu.be | |
| http://youtube.com/watch?v=lalOy8Mbfdc&feature=youtu.be | |
| http://m.youtube.com/watch?v=lalOy8Mbfdc&feature=youtu.be | |
| https://www.youtube.com/watch?v=lalOy8Mbfdc&feature=youtu.be | |
| https://youtube.com/watch?v=lalOy8Mbfdc&feature=youtu.be | |
| https://m.youtube.com/watch?v=lalOy8Mbfdc&feature=youtu.be | |
| http://www.youtube.com/watch?v=dQw4w9WgXcQ&feature=youtube_gdata_player | |
| http://youtube.com/watch?v=dQw4w9WgXcQ&feature=youtube_gdata_player | |
| http://m.youtube.com/watch?v=dQw4w9WgXcQ&feature=youtube_gdata_player | |
| https://www.youtube.com/watch?v=dQw4w9WgXcQ&feature=youtube_gdata_player | |
| https://youtube.com/watch?v=dQw4w9WgXcQ&feature=youtube_gdata_player | |
| https://m.youtube.com/watch?v=dQw4w9WgXcQ&feature=youtube_gdata_player | |
| http://www.youtube.com/watch?v=ishbTyLs6ps&list=PLGup6kBfcU7Le5laEaCLgTKtlDcxMqGxZ&index=106&shuffle=2655 | |
| http://youtube.com/watch?v=ishbTyLs6ps&list=PLGup6kBfcU7Le5laEaCLgTKtlDcxMqGxZ&index=106&shuffle=2655 | |
| http://m.youtube.com/watch?v=ishbTyLs6ps&list=PLGup6kBfcU7Le5laEaCLgTKtlDcxMqGxZ&index=106&shuffle=2655 | |
| https://www.youtube.com/watch?v=ishbTyLs6ps&list=PLGup6kBfcU7Le5laEaCLgTKtlDcxMqGxZ&index=106&shuffle=2655 | |
| https://youtube.com/watch?v=ishbTyLs6ps&list=PLGup6kBfcU7Le5laEaCLgTKtlDcxMqGxZ&index=106&shuffle=2655 | |
| https://m.youtube.com/watch?v=ishbTyLs6ps&list=PLGup6kBfcU7Le5laEaCLgTKtlDcxMqGxZ&index=106&shuffle=2655 | |
| http://www.youtube.com/watch?feature=player_embedded&v=dQw4w9WgXcQ | |
| http://youtube.com/watch?feature=player_embedded&v=dQw4w9WgXcQ | |
| http://m.youtube.com/watch?feature=player_embedded&v=dQw4w9WgXcQ | |
| https://www.youtube.com/watch?feature=player_embedded&v=dQw4w9WgXcQ | |
| https://youtube.com/watch?feature=player_embedded&v=dQw4w9WgXcQ | |
| https://m.youtube.com/watch?feature=player_embedded&v=dQw4w9WgXcQ | |
| http://www.youtube.com/watch?app=desktop&v=dQw4w9WgXcQ | |
| http://youtube.com/watch?app=desktop&v=dQw4w9WgXcQ | |
| http://m.youtube.com/watch?app=desktop&v=dQw4w9WgXcQ | |
| https://www.youtube.com/watch?app=desktop&v=dQw4w9WgXcQ | |
| https://youtube.com/watch?app=desktop&v=dQw4w9WgXcQ | |
| https://m.youtube.com/watch?app=desktop&v=dQw4w9WgXcQ | |
| http://www.youtube.com/watch/-wtIMTCHWuI | |
| http://youtube.com/watch/-wtIMTCHWuI | |
| http://m.youtube.com/watch/-wtIMTCHWuI | |
| https://www.youtube.com/watch/-wtIMTCHWuI | |
| https://youtube.com/watch/-wtIMTCHWuI | |
| https://m.youtube.com/watch/-wtIMTCHWuI | |
| http://www.youtube.com/watch/-wtIMTCHWuI?app=desktop | |
| http://youtube.com/watch/-wtIMTCHWuI?app=desktop | |
| http://m.youtube.com/watch/-wtIMTCHWuI?app=desktop | |
| https://www.youtube.com/watch/-wtIMTCHWuI?app=desktop | |
| https://youtube.com/watch/-wtIMTCHWuI?app=desktop | |
| https://m.youtube.com/watch/-wtIMTCHWuI?app=desktop | |
| http://www.youtube.com/v/dQw4w9WgXcQ | |
| http://youtube.com/v/dQw4w9WgXcQ | |
| http://m.youtube.com/v/dQw4w9WgXcQ | |
| https://www.youtube.com/v/dQw4w9WgXcQ | |
| https://youtube.com/v/dQw4w9WgXcQ | |
| https://m.youtube.com/v/dQw4w9WgXcQ | |
| http://www.youtube.com/v/-wtIMTCHWuI?version=3&autohide=1 | |
| http://youtube.com/v/-wtIMTCHWuI?version=3&autohide=1 | |
| http://m.youtube.com/v/-wtIMTCHWuI?version=3&autohide=1 | |
| https://www.youtube.com/v/-wtIMTCHWuI?version=3&autohide=1 | |
| https://youtube.com/v/-wtIMTCHWuI?version=3&autohide=1 | |
| https://m.youtube.com/v/-wtIMTCHWuI?version=3&autohide=1 | |
| http://www.youtube.com/v/0zM3nApSvMg?fs=1&hl=en_US&rel=0 | |
| http://youtube.com/v/0zM3nApSvMg?fs=1&hl=en_US&rel=0 | |
| http://m.youtube.com/v/0zM3nApSvMg?fs=1&hl=en_US&rel=0 | |
| https://www.youtube.com/v/0zM3nApSvMg?fs=1&hl=en_US&rel=0 | |
| https://www.youtube.com/v/0zM3nApSvMg?fs=1&hl=en_US&rel=0 | |
| https://youtube.com/v/0zM3nApSvMg?fs=1&hl=en_US&rel=0 | |
| https://m.youtube.com/v/0zM3nApSvMg?fs=1&hl=en_US&rel=0 | |
| http://www.youtube.com/v/dQw4w9WgXcQ?feature=youtube_gdata_player | |
| http://youtube.com/v/dQw4w9WgXcQ?feature=youtube_gdata_player | |
| http://m.youtube.com/v/dQw4w9WgXcQ?feature=youtube_gdata_player | |
| https://www.youtube.com/v/dQw4w9WgXcQ?feature=youtube_gdata_player | |
| https://youtube.com/v/dQw4w9WgXcQ?feature=youtube_gdata_player | |
| https://m.youtube.com/v/dQw4w9WgXcQ?feature=youtube_gdata_player | |
| http://youtu.be/-wtIMTCHWuI | |
| https://youtu.be/-wtIMTCHWuI | |
| http://youtu.be/dQw4w9WgXcQ?feature=youtube_gdata_player | |
| https://youtu.be/dQw4w9WgXcQ?feature=youtube_gdata_player | |
| http://youtu.be/oTJRivZTMLs?list=PLToa5JuFMsXTNkrLJbRlB--76IAOjRM9b | |
| https://youtu.be/oTJRivZTMLs?list=PLToa5JuFMsXTNkrLJbRlB--76IAOjRM9b | |
| http://youtu.be/oTJRivZTMLs&feature=channel | |
| https://youtu.be/oTJRivZTMLs&feature=channel | |
| http://youtu.be/lalOy8Mbfdc?t=1 | |
| http://youtu.be/lalOy8Mbfdc?t=1s | |
| https://youtu.be/lalOy8Mbfdc?t=1 | |
| https://youtu.be/lalOy8Mbfdc?t=1s | |
| http://youtu.be/M9bq_alk-sw?si=B_RZg_I-lLaa7UU- | |
| https://youtu.be/M9bq_alk-sw?si=B_RZg_I-lLaa7UU- | |
| http://www.youtube.com/oembed?url=http%3A//www.youtube.com/watch?v%3D-wtIMTCHWuI&format=json | |
| http://youtube.com/oembed?url=http%3A//www.youtube.com/watch?v%3D-wtIMTCHWuI&format=json | |
| http://m.youtube.com/oembed?url=http%3A//www.youtube.com/watch?v%3D-wtIMTCHWuI&format=json | |
| https://www.youtube.com/oembed?url=http%3A//www.youtube.com/watch?v%3D-wtIMTCHWuI&format=json | |
| https://youtube.com/oembed?url=http%3A//www.youtube.com/watch?v%3D-wtIMTCHWuI&format=json | |
| https://m.youtube.com/oembed?url=http%3A//www.youtube.com/watch?v%3D-wtIMTCHWuI&format=json | |
| http://www.youtube.com/attribution_link?a=JdfC0C9V6ZI&u=%2Fwatch%3Fv%3DEhxJLojIE_o%26feature%3Dshare | |
| http://youtube.com/attribution_link?a=JdfC0C9V6ZI&u=%2Fwatch%3Fv%3DEhxJLojIE_o%26feature%3Dshare | |
| http://m.youtube.com/attribution_link?a=JdfC0C9V6ZI&u=%2Fwatch%3Fv%3DEhxJLojIE_o%26feature%3Dshare | |
| https://www.youtube.com/attribution_link?a=JdfC0C9V6ZI&u=%2Fwatch%3Fv%3DEhxJLojIE_o%26feature%3Dshare | |
| https://youtube.com/attribution_link?a=JdfC0C9V6ZI&u=%2Fwatch%3Fv%3DEhxJLojIE_o%26feature%3Dshare | |
| https://m.youtube.com/attribution_link?a=JdfC0C9V6ZI&u=%2Fwatch%3Fv%3DEhxJLojIE_o%26feature%3Dshare | |
| http://www.youtube.com/attribution_link?a=8g8kPrPIi-ecwIsS&u=/watch%3Fv%3DyZv2daTWRZU%26feature%3Dem-uploademail | |
| http://youtube.com/attribution_link?a=8g8kPrPIi-ecwIsS&u=/watch%3Fv%3DyZv2daTWRZU%26feature%3Dem-uploademail | |
| http://m.youtube.com/attribution_link?a=8g8kPrPIi-ecwIsS&u=/watch%3Fv%3DyZv2daTWRZU%26feature%3Dem-uploademail | |
| https://www.youtube.com/attribution_link?a=8g8kPrPIi-ecwIsS&u=/watch%3Fv%3DyZv2daTWRZU%26feature%3Dem-uploademail | |
| https://youtube.com/attribution_link?a=8g8kPrPIi-ecwIsS&u=/watch%3Fv%3DyZv2daTWRZU%26feature%3Dem-uploademail | |
| https://m.youtube.com/attribution_link?a=8g8kPrPIi-ecwIsS&u=/watch%3Fv%3DyZv2daTWRZU%26feature%3Dem-uploademail | |
| http://www.youtube.com/embed/lalOy8Mbfdc | |
| http://youtube.com/embed/lalOy8Mbfdc | |
| http://m.youtube.com/embed/lalOy8Mbfdc | |
| https://www.youtube.com/embed/lalOy8Mbfdc | |
| https://youtube.com/embed/lalOy8Mbfdc | |
| https://m.youtube.com/embed/lalOy8Mbfdc | |
| http://www.youtube.com/embed/nas1rJpm7wY?rel=0 | |
| http://youtube.com/embed/nas1rJpm7wY?rel=0 | |
| http://m.youtube.com/embed/nas1rJpm7wY?rel=0 | |
| https://www.youtube.com/embed/nas1rJpm7wY?rel=0 | |
| https://youtube.com/embed/nas1rJpm7wY?rel=0 | |
| https://m.youtube.com/embed/nas1rJpm7wY?rel=0 | |
| http://www.youtube-nocookie.com/embed/lalOy8Mbfdc?rel=0 | |
| https://www.youtube-nocookie.com/embed/lalOy8Mbfdc?rel=0 | |
| http://www.youtube.com/e/dQw4w9WgXcQ | |
| http://youtube.com/e/dQw4w9WgXcQ | |
| http://m.youtube.com/e/dQw4w9WgXcQ | |
| https://www.youtube.com/e/dQw4w9WgXcQ | |
| https://youtube.com/e/dQw4w9WgXcQ | |
| https://m.youtube.com/e/dQw4w9WgXcQ | |
| http://www.youtube.com/shorts/j9rZxAF3C0I | |
| http://youtube.com/shorts/j9rZxAF3C0I | |
| http://m.youtube.com/shorts/j9rZxAF3C0I | |
| https://www.youtube.com/shorts/j9rZxAF3C0I | |
| https://youtube.com/shorts/j9rZxAF3C0I | |
| https://m.youtube.com/shorts/j9rZxAF3C0I | |
| http://www.youtube.com/shorts/j9rZxAF3C0I?app=desktop | |
| http://youtube.com/shorts/j9rZxAF3C0I?app=desktop | |
| http://m.youtube.com/shorts/j9rZxAF3C0I?app=desktop | |
| https://www.youtube.com/shorts/j9rZxAF3C0I?app=desktop | |
| https://youtube.com/shorts/j9rZxAF3C0I?app=desktop | |
| https://m.youtube.com/shorts/j9rZxAF3C0I?app=desktop | |
| http://www.youtube.com/live/8hBmepWUJoc | |
| http://youtube.com/live/8hBmepWUJoc | |
| http://m.youtube.com/live/8hBmepWUJoc | |
| https://www.youtube.com/live/8hBmepWUJoc | |
| https://youtube.com/live/8hBmepWUJoc | |
| https://m.youtube.com/live/8hBmepWUJoc | |
| http://www.youtube.com/live/8hBmepWUJoc?app=desktop | |
| http://youtube.com/live/8hBmepWUJoc?app=desktop | |
| http://m.youtube.com/live/8hBmepWUJoc?app=desktop | |
| https://www.youtube.com/live/8hBmepWUJoc?app=desktop | |
| https://youtube.com/live/8hBmepWUJoc?app=desktop | |
| https://m.youtube.com/live/8hBmepWUJoc?app=desktop |
| https://youtube.com/user/GitHub#p/a/u/1/lalOy8Mbfdc | |
| https://www.youtube.com/user/GitHub#p/u/1/lalOy8Mbfdc | |
| https://www.youtube.com/user/GitHub#p/u/1/lalOy8Mbfdc?rel=0 | |
| https://www.youtube.com/user/GitHub#p/a/u/2/lalOy8Mbfdc | |
| https://www.youtube.com/user/GitHub#p/u/11/lalOy8Mbfdc | |
| https://www.youtube.com/user/GitHub#p/u/1/lalOy8Mbfdc | |
| https://www.youtube.com/GitHub?v=lalOy8Mbfdc | |
| http://www.youtube.com/?v=dQw4w9WgXcQ | |
| http://youtube.com/?v=dQw4w9WgXcQ&feature=youtube_gdata_player | |
| http://youtube.com/?v=lalOy8Mbfdc&feature=channel | |
| http://youtube.com/?vi=dQw4w9WgXcQ | |
| http://youtube.com/?vi=dQw4w9WgXcQ&feature=youtube_gdata_player | |
| http://youtube.com/?vi=lalOy8Mbfdc&feature=channel | |
| http://www.youtube.com/?feature=player_embedded&v=dQw4w9WgXcQ | |
| http://youtube.com/?feature=channel&v=lalOy8Mbfdc | |
| http://youtube.com/watch?vi=dQw4w9WgXcQ | |
| http://youtube.com/watch?vi=dQw4w9WgXcQ&feature=youtube_gdata_player | |
| http://youtube.com/watch?vi=lalOy8Mbfdc&feature=channel | |
| http://youtube.com/vi/dQw4w9WgXcQ | |
| http://youtube.com/vi/dQw4w9WgXcQ?feature=youtube_gdata_player | |
| http://youtube.com/vi/lalOy8Mbfdc&feature=channel | |
| http://www.youtube-nocookie.com/v/6L3ZvIMwZFM?version=3&hl=en_US&rel=0 |
@ZGorlock: doesn't match music.youtube.com/watch?v=U9t-slLl30E, youtube.com/watch/U9t-slLl30E, youtube.com/shorts/sMbxjePPmkw and attribution_links (youtube.com/attribution_link?a=JdfC0C9V6ZI&u=%2Fwatch%3Fv%3DEhxJLojIE_o%26feature%3Dshare and youtube.com/attribution_link?a=8g8kPrPIi-ecwIsS&u=/watch%3Fv%3DyZv2daTWRZU%26feature%3Dem-uploademail). 🤔
@rodrigoborgesdeoliveira Please add links youtube.com/watch/U9t-slLl30E, youtube.com/shorts/U9t-slLl30E and youtube.com/live/U9t-slLl30E. Here's my RegExp that catches a wide spectrum of YT URLs → https://gist.github.com/Kaligula0/1ff5f4e2cf1f351daeca3450f71fdcb5
Thanks, @Kaligula0!
Thanks @rodrigoborgesdeoliveira!
Here is a JavaScript version - it returns the 13 IDs found in the list you posted
const delParms = ['a', 'app', 'list', 'feature', 'rel', 'si'];
const ids = new Set(
list.map(url => {
// Normalize URL
url = url.replace(/(attribution_link|uploademail|youtube-nocookie)/gi, 'x');
// Parse URL
let urlObject = new URL(url);
// Check for embedded URL and parse it if present
const embed = urlObject.searchParams.get('url');
if (embed) urlObject = new URL(embed);
// Extract 'v' parameter if present
const v = urlObject.searchParams.get('v');
if (v) return v;
// Remove unwanted parameters
delParms.forEach(parm => urlObject.searchParams.delete(parm));
// Convert URL to string and replace encoded '='
url = urlObject.toString().replace(/%3D/gi, '=');
// Extract ID from URL
const idMatch = url.match(/[\-_a-z0-9]{10,15}/i);
const id = idMatch ? idMatch[0] : `No: ${url}`;
return id;
})
);
console.log(ids);
Here's a python regex that matches all of the links in this gist:
^((https?://(?:www\.)?(?:m\.)?youtube\.com))/((?:oembed\?url=https?%3A//(?:www\.)youtube.com/watch\?(?:v%3D)(?P<video_id_1>[\w\-]{10,20})&format=json)|(?:attribution_link\?a=.*watch(?:%3Fv%3D|%3Fv%3D)(?P<video_id_2>[\w\-]{10,20}))(?:%26feature.*))|(https?:)?(\/\/)?((www\.|m\.)?youtube(-nocookie)?\.com\/((watch)?\?(app=desktop&)?(feature=\w*&)?v=|embed\/|v\/|e\/)|youtu\.be\/)(?P<video_id_3>[\w\-]{10,20})Python 3 implementation:
def get_youtube_video_id_by_url(url): regex = r"^((https?://(?:www\.)?(?:m\.)?youtube\.com))/((?:oembed\?url=https?%3A//(?:www\.)youtube.com/watch\?(?:v%3D)(?P<video_id_1>[\w\-]{10,20})&format=json)|(?:attribution_link\?a=.*watch(?:%3Fv%3D|%3Fv%3D)(?P<video_id_2>[\w\-]{10,20}))(?:%26feature.*))|(https?:)?(\/\/)?((www\.|m\.)?youtube(-nocookie)?\.com\/((watch)?\?(app=desktop&)?(feature=\w*&)?v=|embed\/|v\/|e\/)|youtu\.be\/)(?P<video_id_3>[\w\-]{10,20})" match = re.match(regex, url, re.IGNORECASE) if match: return ( match.group("video_id_1") or match.group("video_id_2") or match.group("video_id_3") ) else: return NoneIt matches everything in the gist, but can probably be improved upon for performance, so feel free to suggest improvements! Just test them here first: https://regex101.com/
Thank you for sharing this snippet. Working great.
Just FYI both these functions do not work for URLs in the following format.
http://www.youtube.com/watch/-wtIMTCHWuI
http://youtube.com/watch/-wtIMTCHWuI
http://m.youtube.com/watch/-wtIMTCHWuI
https://www.youtube.com/watch/-wtIMTCHWuI
https://youtube.com/watch/-wtIMTCHWuI
https://m.youtube.com/watch/-wtIMTCHWuI
For those using javascript/typescript, I have a function and regex working for all combinations above plus youtube shorts:
export function getVideoIdFromYoutubeUrl(url: string) { const regex = /(?:youtu\.be\/|youtube\.com(?:\/(?:[^\/\n\s]+\/\S+\/|(?:v|e(?:mbed)?)\/|\S*?[?&]v=|shorts\/)|youtu\.be\/|embed\/|v\/|m\/|watch\?(?:[^=]+=[^&]+&)*?v=))([^"&?\/\s]{11})/gm; return regex.exec(url)[1]; }
Here's a python regex that matches all of the links in this gist:
^((https?://(?:www\.)?(?:m\.)?youtube\.com))/((?:oembed\?url=https?%3A//(?:www\.)youtube.com/watch\?(?:v%3D)(?P<video_id_1>[\w\-]{10,20})&format=json)|(?:attribution_link\?a=.*watch(?:%3Fv%3D|%3Fv%3D)(?P<video_id_2>[\w\-]{10,20}))(?:%26feature.*))|(https?:)?(\/\/)?((www\.|m\.)?youtube(-nocookie)?\.com\/((watch)?\?(app=desktop&)?(feature=\w*&)?v=|embed\/|v\/|e\/)|youtu\.be\/)(?P<video_id_3>[\w\-]{10,20})Python 3 implementation:
def get_youtube_video_id_by_url(url): regex = r"^((https?://(?:www\.)?(?:m\.)?youtube\.com))/((?:oembed\?url=https?%3A//(?:www\.)youtube.com/watch\?(?:v%3D)(?P<video_id_1>[\w\-]{10,20})&format=json)|(?:attribution_link\?a=.*watch(?:%3Fv%3D|%3Fv%3D)(?P<video_id_2>[\w\-]{10,20}))(?:%26feature.*))|(https?:)?(\/\/)?((www\.|m\.)?youtube(-nocookie)?\.com\/((watch)?\?(app=desktop&)?(feature=\w*&)?v=|embed\/|v\/|e\/)|youtu\.be\/)(?P<video_id_3>[\w\-]{10,20})" match = re.match(regex, url, re.IGNORECASE) if match: return ( match.group("video_id_1") or match.group("video_id_2") or match.group("video_id_3") ) else: return None
to cover these edge cases, you can use the following regex:
/(?:https?:\/\/)?(?:www\.)?(?:m\.)?youtube\.com\/watch\/([a-zA-Z0-9_-]+)/and in typescript:
export function getVideoIdFromYoutubeUrl(url: string) {
const regex =
/(?:youtu\.be\/|youtube\.com(?:\/(?:[^\/\n\s]+\/\S+\/|(?:v|e(?:mbed)?)\/|\S*?[?&]v=|shorts\/)|youtu\.be\/|embed\/|v\/|m\/|watch\?(?:[^=]+=[^&]+&)*?v=))([^"&?\/\s]{11})/gm;
const edgeCasesRegex = /(?:https?:\/\/)?(?:www\.)?(?:m\.)?youtube\.com\/watch\/([a-zA-Z0-9_-]+)/;
let result = regex.exec(url);
if (!result) result = edgeCasesRegex.exec(url);
return result[1];
}i need help with this one https://www.youtube.com/watch?v=PbUVdRzzq4E
if you are using JS, TS, or Angular u can use this works fine for me
getYoutubeVideoId(url) {
// Regular expression to match different YouTube URL formats, including shorts
const regExp = /(?:youtube\.com\/(?:[^\/\n\s]+\/\S+\/|(?:v|e(?:mbed)?|shorts)\/|\S*?[?&]v=)|youtu\.be\/)([a-zA-Z0-9_-]{11})/i;
const match = url.match(regExp);
console.log(match && match[1] ? match[1] : null, "id", url); // For debugging purposes
return match && match[1] ? match[1] : null;
}
If you trying to find a regex to match all format YT links try this
I write it for myself, it's works for me.
I use Js.
I love this when rickroll is covered
Another approach is to first verify that the string contains a YouTube domain:
/(?:https?:\/\/(www.|m.)?)?(?<domain>youtu.be|youtube.com)(?:\/)/Then pick out the data you're looking for. For me, I'm only interested in the video ID:
/((?:\/|v=|vi=|v%)(?<id>([\-\w]){10,14}))/Thanks @rodrigoborgesdeoliveira for your URL list!
(YouTube 2025)
V█^(?:(.*?)youtu(?<2>\.)?be(?:-nocookie)?(?-i:\.com\/(?:[ev]i?\/|\?vi?=)?|shorts\/|watch(?:[/&?]|vi?=|t=\d++|time_continue=\d+&|[?&]gcm[eslg]+=\d+)+|live\/|o?embed(?:\?url=\S+?|\/)|(?:%[23][56FD]v?|watch){2,}|attribution_link\?[&\w\-=]*[au]=\/?|ytsc\w+\?v=|[?featurp]+=[\-a-z_\.]+&v=)*(?(<2>)\/|)|(?-is:(?:YTv|YouTube)[ :=]+)|(?-i:[a-z]:|\\))?([a-zA-Z_0-9-]{10}[AEIMQUYcgkosw048])(?=[ \r\n\?&"%\]]|\Z|#t=)
>█\3 •
https://m.youtube.com/watch%3Fv%3DAAAAAAAAAAA&ved=2ahUKEwjkrsSipPP1AhXsyjGHb0CDU4Qx8BegQIBRAF&usg=AOvVaw0A7aB50nUUeTwX_Haoteft
http//www.youtube-nocookie.com/embed/AAAAAAAAAAA?rel=0
http://www.youtube-nocookie.com/v/AAAAAAAAAAA?version=3&hl=en_US&rel=0
http://www.youtube.com/?feature=player_embedded&v=AAAAAAAAAAA
http://www.youtube.com/embed/watch?feature=player_embedded&v=AAAAAAAAAAA
http://www.youtube.com/watch?feature=player_embedded&v=AAAAAAAAAAA
https://www.youtube.com/embed/watch?feature=player_embedded&v=AAAAAAAAAAA
https://m.youtube.com/watch?app=desktop&v=AAAAAAAAAAA
http://www.youtube.com/attribution_link?a=JdfC0C9V6ZI&u=%2Fwatch%3Fv%3DAAAAAAAAAAA%26feature%3Dshare
http://www.youtube.com/attribution_link?u=/watch?v=AAAAAAAAAAA&feature=share
https://www.youtube.com/attribution_link?a=8g8kPrPIi-ecwIsS&u=/watch%3Fv%3DAAAAAAAAAAA%26feature%3Dem-uploademail
http://www.youtube.com/e/AAAAAAAAAAA
http://www.youtube.com/embed/AAAAAAAAAAA
http://www.youtube.com/embed/AAAAAAAAAAA?rel=0
http://www.youtube.com/oembed?url=http%3A//www.youtube.com/watch?v%3DAAAAAAAAAAA&format=json
http://www.youtube.com/v/AAAAAAAAAAA
http://www.youtube.com/v/AAAAAAAAAAA?fs=1&hl=en_US&rel=0
http://www.youtube.com/v/AAAAAAAAAAA?version=3&autohide=1
www.youtube.com/watch?v=AAAAAAAAAAA
http://www.youtube.com/watch?v=AAAAAAAAAAA
http://www.youtube.com/watch?v=AAAAAAAAAAA#t=0m10s
http://www.youtube.com/watch?v=AAAAAAAAAAA&feature=feedrec_grec_index
http://www.youtube.com/watch?v=AAAAAAAAAAA&feature=youtu.be
https://www.youtube.com/watch?t=2724&v=AAAAAAAAAAA&feature=youtu.be
https://www.youtube.com/watch?v=AAAAAAAAAAA#t=0m10s
http://www.youtube.com/watch?v=AAAAAAAAAAA&feature=youtube_gdata_player
https://www.youtube.com/watch?v=AAAAAAAAAAA&feature=em-uploademail
https://www.youtube.com/watch?v=AAAAAAAAAAA&feature=feedrec_grec_index
https://www.youtube.com/watch?v=AAAAAAAAAAA&list=PLGup6kBfcU7Le5laEaCLgTKtlDcxMqGxZ&index=106&shuffle=2655
http://www.youtube.com/watch?v=AAAAAAAAAAA&playnext_from=TL&videos=osPknwzXEas&feature=sub
http://www.youtube.com/ytscreeningroom?v=AAAAAAAAAAA
youtu.be/AAAAAAAAAAA
youtube.com/watch?v=AAAAAAAAAAA
http://youtu.be/AAAAAAAAAAA?feature=youtube_gdata_player
http://youtube.com/?feature=channel&v=AAAAAAAAAAA
http://www.youtube.com/?v=AAAAAAAAAAA&feature=youtube_gdata_player
http://youtube.com/?vi=AAAAAAAAAAA&feature=youtube_gdata_player
http://youtube.com/embed/AAAAAAAAAAA
http://youtube.com/v/AAAAAAAAAAA?feature=youtube_gdata_player
http://youtube.com/vi/AAAAAAAAAAA&feature=channel
http://youtube.com/vi/AAAAAAAAAAA?feature=youtube_gdata_player
http://youtube.com/watch?v=AAAAAAAAAAA&feature=youtube_gdata_player
http://youtube.com/watch?vi=AAAAAAAAAAA&feature=youtube_gdata_player
https://m.youtube.com/watch?v=AAAAAAAAAAA
https://www.youtube-nocookie.com/embed/AAAAAAAAAAA?rel=0
https://www.youtube.com/embed/watch?v=AAAAAAAAAAA
https://www.youtube.com/embed/AAAAAAAAAAA?rel=0
https://www.youtube.com/v/AAAAAAAAAAA?fs=1&hl=en_US&rel=0
https://www.youtube.com/watch/AAAAAAAAAAA
https://youtu.be/AAAAAAAAAAA?list=PLToa5JuFMsXTNkrLJbRlB--76IAOjRM9b
https://youtube.com/shorts/AAAAAAAAAAA?feature=share
https://youtu.be/x26ANNC3C-8 • ♾ 𝕳𝕰𝕽𝕰𝕿𝕳𝕰𝖄𝕮𝕺𝕸𝕰 - 𝔩𝔢𝔞𝔳𝔢 𝔪𝔢 𝔞𝔩𝔬𝔫𝔢 • 7:15
https://www.youtube.com/watch?time_continue=88&v=AAAAAAAAAAA&feature=emb_title
/// will not match
https://www.youtube.com/embed/v=AAAAAAAAAAA
http://www.youtube.com/user/ccccccccccc#p/u/1/AAAAAAAAAAA
http://www.youtube.com/user/ccccccccccc#p/u/1/AAAAAAAAAAA?rel=0
https://www.youtube.com/feed/subscriptions
https://www.youtube.com/channel/UCgc00bfF_PvO_2AvqJZHXFg
https://www.youtube.com/c/ccccccccccc/videos
http://www.youtube.com/user/ccccccccccc#p/u/11/AAAAAAAAAAA
https://www.youtube.com/user/ccccccccccc#p/a/u/1/AAAAAAAAAAA
http://www.youtube.com/user/ccccccccccc#p/a/u/2/AAAAAAAAAAA
https://www.youtube.com/source/Qap6-GsibqA/shorts?bp=8gVeClISQwoLUWFwNi1Hc2licUESC1FhcDYtR3NpYnFBGgt3RzQwU1EzWEhCZyIOCLiUARIICBMQgMq17gEqChIICBMQgMq17gEaC3dHNDBTUTNYSEJnKP3a39rFq6HHLA%253D%253D
YouTube: AAAAAAAAAAA
YTv:AAAAAAAAAAA
YTv: AAAAAAAAAAA
YTv AAAAAAAAAAA
https://www.youtube.com/live/AAAAAAAAAAA?feature=share
https://youtu.be/AAAAAAAAAAA?si=eP5r-sAojFjjVYCY
| video ID [-_A-Za-z0-9]{10}[AEIMQUYcgkosw048]
| Channel/Playlist ID [-_A-Za-z0-9]{21}[AQgw]
mariadb function:
DELIMITER //
CREATE FUNCTION youtube_video_id(url TEXT)
RETURNS CHAR(11)
DETERMINISTIC
BEGIN
DECLARE clean TEXT;
DECLARE vid VARCHAR(50);
DECLARE result CHAR(11);
-- Handle NULL or empty input
IF url IS NULL OR url = '' THEN
RETURN NULL;
END IF;
-- Normalize URL
SET clean = TRIM(url);
SET clean = REPLACE(clean, ' ', '');
-- URL decode common patterns (%3F = ?, %3D = =, %26 = &)
SET clean = REPLACE(clean, '%3F', '?');
SET clean = REPLACE(clean, '%3D', '=');
SET clean = REPLACE(clean, '%26', '&');
SET clean = REPLACE(clean, '%2F', '/');
-- Handle URLs with Unicode/special characters after the video ID
-- Extract everything before special Unicode characters
SET clean = SUBSTRING_INDEX(clean, '•', 1);
SET clean = TRIM(clean);
-- Extract video ID based on URL pattern (priority order)
SET vid = CASE
-- youtu.be/VIDEOID (short link) - handle both with and without protocol
WHEN clean REGEXP '(youtu\\.be/|youtu\\.be\\/)' THEN
SUBSTRING_INDEX(SUBSTRING_INDEX(SUBSTRING_INDEX(clean, 'youtu.be/', -1), '?', 1), '&', 1)
-- /shorts/VIDEOID (YouTube Shorts)
WHEN clean REGEXP '/shorts/' THEN
SUBSTRING_INDEX(SUBSTRING_INDEX(SUBSTRING_INDEX(clean, '/shorts/', -1), '?', 1), '&', 1)
-- /watch/VIDEOID (alternative watch format)
WHEN clean REGEXP '/watch/' AND clean NOT REGEXP '/watch\\?' THEN
SUBSTRING_INDEX(SUBSTRING_INDEX(SUBSTRING_INDEX(clean, '/watch/', -1), '?', 1), '&', 1)
-- /embed/VIDEOID (embedded player) - but NOT /embed/watch (handled by v= param)
WHEN clean REGEXP '/embed/' AND clean NOT REGEXP '/embed/watch' THEN
SUBSTRING_INDEX(SUBSTRING_INDEX(SUBSTRING_INDEX(clean, '/embed/', -1), '?', 1), '&', 1)
-- /v/VIDEOID (old embed format)
WHEN clean REGEXP '/v/' THEN
SUBSTRING_INDEX(SUBSTRING_INDEX(SUBSTRING_INDEX(clean, '/v/', -1), '?', 1), '&', 1)
-- /e/VIDEOID (rare format)
WHEN clean REGEXP '/e/' THEN
SUBSTRING_INDEX(SUBSTRING_INDEX(SUBSTRING_INDEX(clean, '/e/', -1), '?', 1), '&', 1)
-- /vi/VIDEOID (video image/thumbnail format)
WHEN clean REGEXP '/vi/' THEN
SUBSTRING_INDEX(SUBSTRING_INDEX(SUBSTRING_INDEX(clean, '/vi/', -1), '?', 1), '&', 1)
-- /ytscreeningroom?v=VIDEOID
WHEN clean REGEXP '/ytscreeningroom\\?v=' THEN
SUBSTRING_INDEX(SUBSTRING_INDEX(clean, 'v=', -1), '&', 1)
-- #p/u/NUMBER/VIDEOID (user playlist fragment)
WHEN clean REGEXP '#p/u/[0-9]+/' THEN
SUBSTRING_INDEX(SUBSTRING_INDEX(clean, '#p/u/', -1), '/', -1)
-- #p/c/PLAYLIST/NUMBER/VIDEOID (channel playlist fragment)
WHEN clean REGEXP '#p/c/[^/]+/[0-9]+/' THEN
SUBSTRING_INDEX(clean, '/', -1)
-- /attribution_link with u=/watch?v= or u=%2Fwatch%3Fv%3D
WHEN clean REGEXP '/attribution_link' AND clean REGEXP 'u=' THEN
CASE
WHEN clean REGEXP 'u=/watch\\?v=' THEN
SUBSTRING_INDEX(SUBSTRING_INDEX(SUBSTRING_INDEX(clean, 'u=/watch?v=', -1), '&', 1), '#', 1)
WHEN clean REGEXP 'u=%2Fwatch%3Fv%3D' THEN
SUBSTRING_INDEX(SUBSTRING_INDEX(clean, 'u=%2Fwatch%3Fv%3D', -1), '%26', 1)
ELSE NULL
END
-- watch?v=VIDEOID or &v=VIDEOID (standard watch URL)
WHEN clean REGEXP '[?&]v=' THEN
SUBSTRING_INDEX(SUBSTRING_INDEX(clean, 'v=', -1), '&', 1)
-- vi=VIDEOID parameter (alternative format)
WHEN clean REGEXP '[?&]vi=' THEN
SUBSTRING_INDEX(SUBSTRING_INDEX(clean, 'vi=', -1), '&', 1)
-- No match
ELSE NULL
END;
-- Clean extracted ID: remove trailing slashes, parameters, fragments
IF vid IS NOT NULL THEN
SET vid = SUBSTRING_INDEX(vid, '/', 1);
SET vid = SUBSTRING_INDEX(vid, '?', 1);
SET vid = SUBSTRING_INDEX(vid, '#', 1);
SET vid = SUBSTRING_INDEX(vid, '&', 1);
SET vid = SUBSTRING_INDEX(vid, '%', 1);
SET vid = TRIM(vid);
END IF;
-- Validate: must be exactly 11 characters and contain valid YouTube ID characters
IF vid IS NOT NULL
AND CHAR_LENGTH(vid) = 11
AND vid REGEXP '^[A-Za-z0-9_-]{11}$' THEN
SET result = vid;
ELSE
SET result = NULL;
END IF;
RETURN result;
END//
DELIMITER ;testing
SELECT
url,
youtube_video_id(url) AS video_id
FROM (
SELECT 'https://m.youtube.com/watch%3Fv%3DAAAAAAAAAAA&ved=2ahUKEwjkrsSipPP1AhXsyjGHb0CDU4Qx8BegQIBRAF&usg=AOvVaw0A7aB50nUUeTwX_Haoteft' AS url UNION ALL
SELECT 'http//www.youtube-nocookie.com/embed/AAAAAAAAAAA?rel=0' UNION ALL
SELECT 'http://www.youtube-nocookie.com/v/AAAAAAAAAAA?version=3&hl=en_US&rel=0' UNION ALL
SELECT 'http://www.youtube.com/?feature=player_embedded&v=AAAAAAAAAAA' UNION ALL
SELECT 'http://www.youtube.com/embed/watch?feature=player_embedded&v=AAAAAAAAAAA' UNION ALL
SELECT 'http://www.youtube.com/watch?feature=player_embedded&v=AAAAAAAAAAA' UNION ALL
SELECT 'https://www.youtube.com/embed/watch?feature=player_embedded&v=AAAAAAAAAAA' UNION ALL
SELECT 'https://m.youtube.com/watch?app=desktop&v=AAAAAAAAAAA' UNION ALL
SELECT 'http://www.youtube.com/attribution_link?a=JdfC0C9V6ZI&u=%2Fwatch%3Fv%3DAAAAAAAAAAA%26feature%3Dshare' UNION ALL
SELECT 'http://www.youtube.com/attribution_link?u=/watch?v=AAAAAAAAAAA&feature=share' UNION ALL
SELECT 'https://www.youtube.com/attribution_link?a=8g8kPrPIi-ecwIsS&u=/watch%3Fv%3DAAAAAAAAAAA%26feature%3Dem-uploademail' UNION ALL
SELECT 'http://www.youtube.com/e/AAAAAAAAAAA' UNION ALL
SELECT 'http://www.youtube.com/embed/AAAAAAAAAAA' UNION ALL
SELECT 'http://www.youtube.com/embed/AAAAAAAAAAA?rel=0' UNION ALL
SELECT 'http://www.youtube.com/oembed?url=http%3A//www.youtube.com/watch?v%3DAAAAAAAAAAA&format=json' UNION ALL
SELECT 'http://www.youtube.com/v/AAAAAAAAAAA' UNION ALL
SELECT 'http://www.youtube.com/v/AAAAAAAAAAA?fs=1&hl=en_US&rel=0' UNION ALL
SELECT 'http://www.youtube.com/v/AAAAAAAAAAA?version=3&autohide=1' UNION ALL
SELECT 'www.youtube.com/watch?v=AAAAAAAAAAA' UNION ALL
SELECT 'http://www.youtube.com/watch?v=AAAAAAAAAAA' UNION ALL
SELECT 'http://www.youtube.com/watch?v=AAAAAAAAAAA#t=0m10s' UNION ALL
SELECT 'http://www.youtube.com/watch?v=AAAAAAAAAAA&feature=feedrec_grec_index' UNION ALL
SELECT 'http://www.youtube.com/watch?v=AAAAAAAAAAA&feature=youtu.be' UNION ALL
SELECT 'https://www.youtube.com/watch?t=2724&v=AAAAAAAAAAA&feature=youtu.be' UNION ALL
SELECT 'https://www.youtube.com/watch?v=AAAAAAAAAAA#t=0m10s' UNION ALL
SELECT 'http://www.youtube.com/watch?v=AAAAAAAAAAA&feature=youtube_gdata_player' UNION ALL
SELECT 'https://www.youtube.com/watch?v=AAAAAAAAAAA&feature=em-uploademail' UNION ALL
SELECT 'https://www.youtube.com/watch?v=AAAAAAAAAAA&feature=feedrec_grec_index' UNION ALL
SELECT 'https://www.youtube.com/watch?v=AAAAAAAAAAA&list=PLGup6kBfcU7Le5laEaCLgTKtlDcxMqGxZ&index=106&shuffle=2655' UNION ALL
SELECT 'http://www.youtube.com/watch?v=AAAAAAAAAAA&playnext_from=TL&videos=osPknwzXEas&feature=sub' UNION ALL
SELECT 'http://www.youtube.com/ytscreeningroom?v=AAAAAAAAAAA' UNION ALL
SELECT 'youtu.be/AAAAAAAAAAA' UNION ALL
SELECT 'youtube.com/watch?v=AAAAAAAAAAA' UNION ALL
SELECT 'http://youtu.be/AAAAAAAAAAA?feature=youtube_gdata_player' UNION ALL
SELECT 'http://youtube.com/?feature=channel&v=AAAAAAAAAAA' UNION ALL
SELECT 'http://www.youtube.com/?v=AAAAAAAAAAA&feature=youtube_gdata_player' UNION ALL
SELECT 'http://youtube.com/?vi=AAAAAAAAAAA&feature=youtube_gdata_player' UNION ALL
SELECT 'http://youtube.com/embed/AAAAAAAAAAA' UNION ALL
SELECT 'http://youtube.com/v/AAAAAAAAAAA?feature=youtube_gdata_player' UNION ALL
SELECT 'http://youtube.com/vi/AAAAAAAAAAA&feature=channel' UNION ALL
SELECT 'http://youtube.com/vi/AAAAAAAAAAA?feature=youtube_gdata_player' UNION ALL
SELECT 'http://youtube.com/watch?v=AAAAAAAAAAA&feature=youtube_gdata_player' UNION ALL
SELECT 'http://youtube.com/watch?vi=AAAAAAAAAAA&feature=youtube_gdata_player' UNION ALL
SELECT 'https://m.youtube.com/watch?v=AAAAAAAAAAA' UNION ALL
SELECT 'https://www.youtube-nocookie.com/embed/AAAAAAAAAAA?rel=0' UNION ALL
SELECT 'https://www.youtube.com/embed/watch?v=AAAAAAAAAAA' UNION ALL
SELECT 'https://www.youtube.com/embed/AAAAAAAAAAA?rel=0' UNION ALL
SELECT 'https://www.youtube.com/v/AAAAAAAAAAA?fs=1&hl=en_US&rel=0' UNION ALL
SELECT 'https://www.youtube.com/watch/AAAAAAAAAAA' UNION ALL
SELECT 'https://youtu.be/AAAAAAAAAAA?list=PLToa5JuFMsXTNkrLJbRlB--76IAOjRM9b' UNION ALL
SELECT 'https://youtube.com/shorts/AAAAAAAAAAA?feature=share' UNION ALL
SELECT 'https://youtu.be/x26ANNC3C-8 • ♾ 𝕳𝕰𝕽𝕰𝕿𝕳𝕰𝖄𝕮𝕺𝕸𝕰 - 𝔩𝔢𝔞𝔳𝔢 𝔪𝔢 𝔞𝔩𝔬𝔫𝔢 • 7:15' UNION ALL
SELECT 'https://www.youtube.com/watch?time_continue=88&v=AAAAAAAAAAA&feature=emb_title'
) AS t;
Here is a java version of regex that matches all the urls the list:
(?i)^(?:https?://)?(?:(?:www|m)\.)?youtu(?:\.be|be(?:-nocookie)?\.com)/(?:(?:oembed|attribution_link)\?.+?(?:/|%2F)(?=watch))?(?:(?:watch)?(?:\?|%3F)|(?:vi?|live|e(?:mbed)?)/)?(?:(?:[\w\-]+(?:=|%3D)[^=&#]+(?:&(?:amp;)?|%26))*?vi?(?:=|%3D))?(?<id>[\w\-]+)(?:(?:(?:\?|%3F)|(?:&(?:amp;)?|%26)|(?:#|%23))[\w\-]+(?:=|%3D)[^=&#]+)*$