Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save rodrigoborgesdeoliveira/987683cfbfcc8d800192da1e73adc486 to your computer and use it in GitHub Desktop.
Save rodrigoborgesdeoliveira/987683cfbfcc8d800192da1e73adc486 to your computer and use it in GitHub Desktop.
Example of the YouTube videos URL formats
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
@mlatushko
Copy link

I have a function and regex

  • Thank you.

And these:

————————————

V█(?-is)^(?:(?!https?[:\/]|www\.|m(?:usic)?\.yo|youtu\.?be[\/\.]|watch[\/\?]|embed\/|YTv(?:[=: ]+)|YouTube: )\V)*?(?:(?:https?[:\/]+|www\.|m\.|music\.)+youtu(?<1>\.)?be(?:\.com\/|watch|live(?=\/)|o?embed(?:\/|\?url=\S+?)?|\?app=\w+|shorts|attribution_link\?[&\w\-=]*[au]=\/?|ytsc\w+|[\?&]*(?:li)?[ve]i?\b[\/=]|\?feature=[\-a-z_\.]+|[\?&]t(?:ime_continue)?=\d+|-nocookie|%[23][56FD]|\.com\/shorts\/)*(?(<1>)\/|)|YTv(?:[=: ]+)|YouTube: )(?(?<=watch)\/|)([a-zA-Z\d\-_]{11}\b)(?>[\?&#][&#%a-zA-Z\-\d=;_\.]*|&feature=[\-a-z_\.]+)?(?=\s|\Z)

█\2

————————————

@Revnoplex
Copy link

Revnoplex commented Nov 11, 2023

For some reason, youtube blocks access to the site via the 11th one. It brings up the "our systems have detected unusual traffic from your computer network" message but with no captcha

@ZGorlock
Copy link

ZGorlock commented Mar 9, 2024

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)[^=&#]+)*$

String QM = "(?:\\?|%3F)";
String EQ = "(?:=|%3D)";
String AS = "(?:&(?:amp;)?|%26)";
String OT = "(?:#|%23)";
String FS = "(?:/|%2F)";

String PARAM_HEAD = "(?:" + QM + "|" + AS + "|" + OT + ")";
String PARAM_KEY = "[\\w\\-]+";
String PARAM_VAL = "[^=&#]+";
String PARAM = "(?:" + PARAM_KEY + EQ + PARAM_VAL + ")";

Pattern p = Pattern.compile("(?i)^" +
        "(?:https?://)?" +
        "(?:(?:www|m)\\.)?" +
        "youtu(?:\\.be|be(?:-nocookie)?\\.com)/" +
        "(?:(?:oembed|attribution_link)\\?.+?" + FS + "(?=watch))?" +
        "(?:(?:watch)?" + QM + "|" + "(?:vi?|live|e(?:mbed)?)/)?" +
        "(?:(?:" + PARAM + AS + ")*?" + "(?:vi?" + EQ + "))?" +
        "(?<id>" + PARAM_KEY + ")" +
        "(?:" + PARAM_HEAD + PARAM + ")*" +
        "$");

IOUtils.readLines(new BufferedReader(new InputStreamReader(
                new URL("https://gist.githubusercontent.com/rodrigoborgesdeoliveira/987683cfbfcc8d800192da1e73adc486/raw/b15ee49a60d42df67b0d6383517ef3b57e25c9c3/ActiveYouTubeURLFormats.txt")
                        .openConnection().getInputStream()))).stream()
        .filter(StringUtils::isNotBlank)
        .map(p::matcher).filter(Matcher::matches)
        .forEachOrdered(m -> System.out.printf("%-15s %s%n", m.group("id"), m.group()));

@Kaligula0
Copy link

@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). 🤔

@Kaligula0
Copy link

@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

@rodrigoborgesdeoliveira
Copy link
Author

Thanks, @Kaligula0!

@meeeet-dev
Copy link

@mplungjan
Copy link

mplungjan commented May 13, 2024

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);

@jagdish1o1
Copy link

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

It 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.

@felipesilva-plank
Copy link

felipesilva-plank commented Jul 4, 2024

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];
}

@XxxGHOSTX
Copy link

@joshigolu08
Copy link

joshigolu08 commented Jul 23, 2024

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;
  }

@ccauvang
Copy link

ccauvang commented Nov 1, 2024

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.

@khchin93
Copy link

I love this when rickroll is covered

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment