93

I have a textfield where users can write anything.

For example:

Lorem Ipsum is simply dummy text. http://www.youtube.com/watch?v=DUQi_R4SgWo of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. http://www.youtube.com/watch?v=A_6gNZCkajU&feature=relmfu It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.

Now I would like to parse it and find all YouTube video URLs and their ids.

Any idea how that works?

Peter Mortensen
  • 28,342
  • 21
  • 95
  • 123
n00b
  • 14,958
  • 19
  • 52
  • 71
  • 1
    possible duplicate of [Javascript REGEX: How to get youtube video id from URL?](http://stackoverflow.com/questions/3452546/javascript-regex-how-to-get-youtube-video-id-from-url) – T.Todua Jan 20 '15 at 14:14

10 Answers10

292

A YouTube video URL may be encountered in a variety of formats:

  • latest short format: http://youtu.be/NLqAF9hrVbY
  • iframe: http://www.youtube.com/embed/NLqAF9hrVbY
  • iframe (secure): https://www.youtube.com/embed/NLqAF9hrVbY
  • object param: http://www.youtube.com/v/NLqAF9hrVbY?fs=1&hl=en_US
  • object embed: http://www.youtube.com/v/NLqAF9hrVbY?fs=1&hl=en_US
  • watch: http://www.youtube.com/watch?v=NLqAF9hrVbY
  • users: http://www.youtube.com/user/Scobleizer#p/u/1/1p3vcRhsYGo
  • ytscreeningroom: http://www.youtube.com/ytscreeningroom?v=NRHVzbJVx8I
  • any/thing/goes!: http://www.youtube.com/sandalsResorts#p/c/54B8C800269D7C1B/2/PPS-8DMrAn4
  • any/subdomain/too: http://gdata.youtube.com/feeds/api/videos/NLqAF9hrVbY
  • more params: http://www.youtube.com/watch?v=spDj54kf-vY&feature=g-vrec
  • query may have dot: http://www.youtube.com/watch?v=spDj54kf-vY&feature=youtu.be
  • nocookie domain: http://www.youtube-nocookie.com

Here is a PHP function with a commented regex that matches each of these URL forms and converts them to links (if they are not links already):

// Linkify youtube URLs which are not already links.
function linkifyYouTubeURLs($text) {
    $text = preg_replace('~(?#!js YouTubeId Rev:20160125_1800)
        # Match non-linked youtube URL in the wild. (Rev:20130823)
        https?://          # Required scheme. Either http or https.
        (?:[0-9A-Z-]+\.)?  # Optional subdomain.
        (?:                # Group host alternatives.
          youtu\.be/       # Either youtu.be,
        | youtube          # or youtube.com or
          (?:-nocookie)?   # youtube-nocookie.com
          \.com            # followed by
          \S*?             # Allow anything up to VIDEO_ID,
          [^\w\s-]         # but char before ID is non-ID char.
        )                  # End host alternatives.
        ([\w-]{11})        # $1: VIDEO_ID is exactly 11 chars.
        (?=[^\w-]|$)       # Assert next char is non-ID or EOS.
        (?!                # Assert URL is not pre-linked.
          [?=&+%\w.-]*     # Allow URL (query) remainder.
          (?:              # Group pre-linked alternatives.
            [\'"][^<>]*>   # Either inside a start tag,
          | </a>           # or inside <a> element text contents.
          )                # End recognized pre-linked alts.
        )                  # End negative lookahead assertion.
        [?=&+%\w.-]*       # Consume any URL (query) remainder.
        ~ix', '<a href="http://www.youtube.com/watch?v=$1">YouTube link: $1</a>',
        $text);
    return $text;
}

; // End $YouTubeId.

And here is a JavaScript version with the exact same regex (with comments removed):

// Linkify youtube URLs which are not already links.
function linkifyYouTubeURLs(text) {
    var re = /https?:\/\/(?:[0-9A-Z-]+\.)?(?:youtu\.be\/|youtube(?:-nocookie)?\.com\S*?[^\w\s-])([\w-]{11})(?=[^\w-]|$)(?![?=&+%\w.-]*(?:['"][^<>]*>|<\/a>))[?=&+%\w.-]*/ig;
    return text.replace(re,
        '<a href="http://www.youtube.com/watch?v=$1">YouTube link: $1</a>');
}

Notes:

  • The VIDEO_ID portion of the URL is captured in the one and only capture group: $1.
  • If you know that your text does not contain any pre-linked URLs, you can safely remove the negative lookahead assertion which tests for this condition (The assertion beginning with the comment: "Assert URL is not pre-linked.") This will speed up the regex somewhat.
  • The replace string can be modified to suit. The one provided above simply creates a link to the generic "http://www.youtube.com/watch?v=VIDEO_ID" style URL and sets the link text to: "YouTube link: VIDEO_ID".

Edit 2011-07-05: Added - hyphen to ID char class

Edit 2011-07-17: Fixed regex to consume any remaining part (e.g. query) of URL following YouTube ID. Added 'i' ignore-case modifier. Renamed function to camelCase. Improved pre-linked lookahead test.

Edit 2011-07-27: Added new "user" and "ytscreeningroom" formats of YouTube URLs.

Edit 2011-08-02: Simplified/generalized to handle new "any/thing/goes" YouTube URLs.

Edit 2011-08-25: Several modifications:

  • Added a Javascript version of: linkifyYouTubeURLs() function.
  • Previous version had the scheme (HTTP protocol) part optional and thus would match invalid URLs. Made the scheme part required.
  • Previous version used the \b word boundary anchor around the VIDEO_ID. However, this will not work if the VIDEO_ID begins or ends with a - dash. Fixed so that it handles this condition.
  • Changed the VIDEO_ID expression so that it must be exactly 11 characters long.
  • The previous version failed to exclude pre-linked URLs if they had a query string following the VIDEO_ID. Improved the negative lookahead assertion to fix this.
  • Added + and % to character class matching query string.
  • Changed PHP version regex delimiter from: % to a: ~.
  • Added a "Notes" section with some handy notes.

Edit 2011-10-12: YouTube URL host part may now have any subdomain (not just www.).

Edit 2012-05-01: The consume URL section may now allow for '-'.

Edit 2013-08-23: Added additional format provided by @Mei. (The query part may have a . dot.

Edit 2013-11-30: Added additional format provided by @CRONUS: youtube-nocookie.com.

Edit 2016-01-25: Fixed regex to handle error case provided by CRONUS.

Peter Mortensen
  • 28,342
  • 21
  • 95
  • 123
ridgerunner
  • 30,685
  • 4
  • 51
  • 68
  • Thanks for this! However I found out that hyphens are also allowed in the id so (\w{10-12}) should be ([\w-]{10,12}) – cottonBallPaws Jun 28 '11 at 23:21
  • @littleFluffyKitty: I knew that the underscore was ok but the dash is news to me. Is there a spec somewhere which defines these valid-ID characters? – ridgerunner Jul 01 '11 at 13:56
  • 2
    I haven't seen a spec, though I did search one out. I just noticed the dash in some links in the wilds of the internet. For example: http://www.youtube.com/watch?v=CLPk-6_xgiY – cottonBallPaws Jul 05 '11 at 23:20
  • 1
    @littleFluffyKitty: Thanks for the heads up. Have updated the answer to include the hyphen as a valid ID char. – ridgerunner Jul 06 '11 at 00:29
  • @ridgerunner I've come across other URL's like such: http://www.youtube.com/sandalsResorts#p/c/54B8C800269D7C1B/2/PPS-8DMrAn4 and I can't seem to modify your RegEx to catch this one Thanx in advance! – Stanley Aug 01 '11 at 16:05
  • @Stanley: Thanks for the new URL format. The regex is updated to handle this one (and should now work for just about any other one as well). Cheers! – ridgerunner Aug 03 '11 at 02:09
  • @ridgerunner - I just discovered a URL in simple browsing that doesn't seem to work: http://www.youtube.com/watch?v=VERGbYDuJ-4&feature=feedu. Can this one be fixed? Thank you so much - this is quite a project to write the definitive YouTube ID regex! – sscirrus Oct 11 '11 at 08:44
  • @sscirrus - The `linkifyYouTubeURLs()` function does a simple match-and-replace operation which requires that the matched URL be a valid URL so that it can be directly inserted into the `href` attribute of an `A` tag. Thus, the regex above does require the URL scheme (either `http` or `https`). If you are willing to do a bit more logic in the function, then the regex could be modified to match the example you cite (which has no scheme). To do this, simply wrap the scheme expression at the beginning and add a `?` like so: `(?:https?://)?`. Thanks for the comment! – ridgerunner Oct 11 '11 at 13:05
  • @ridgerunner - I finally identified the issue that's been cropping up for me - your Javascript regex above works with everything except the `?v=` format in `watch` and `ytscreeningroom`. I was concerned because copying a regular video's link from the address bar (which most of my users are doing) is triggering this problem. Thank you for your comment yesterday regarding http/s! – sscirrus Oct 12 '11 at 05:33
  • @ridgerunner nice work! Now, how many country specific youtube domains are there and is it reasonable to bake them into this regex? – Rhett Oct 23 '11 at 02:09
  • I think you've missed the possibility of `_` being in the id. – Kevin B Dec 05 '11 at 18:19
  • @Kevin B: No, the `\w` is equivalent to `[A-Za-z0-9_]` which includes the underscore. – ridgerunner Dec 05 '11 at 20:00
  • I can't get this to match http://www.youtube.com/embed/vDGrfhJH1P4&wmode=transparent&autohide=1&egm=0&hd=1&iv_load_policy=3&modestbranding=1&rel=0 Because it will catch transparent and the id. – Doug Tabuchi Mar 06 '12 at 19:56
  • @Doug Tabuchi - this regex requires that the: `http://` or `https://` URL scheme to be present. – ridgerunner Mar 06 '12 at 21:46
  • That was a paste fail. This url matches transparent instead of vDGrfhJH1P4. http://www.youtube.com/embed/vDGrfhJH1P4&wmode=transparent&autohide=1&egm=0&hd=1&iv_load_policy=3&modestbranding=1&rel=0 – Doug Tabuchi Mar 07 '12 at 17:16
  • Also looks like when you paste a link in SO it linkify's it so http:// was present in the paste but is removed by SO. – Doug Tabuchi Mar 07 '12 at 17:18
  • @hakre - So anyone can come in and edit the content of this answer without my notification or approval? I thought this was supposed to be _my_ answer??? If this is the case then I am going to rethink my contributions to this site... – ridgerunner May 03 '12 at 23:00
  • @ridgerunner: I didn't wanted to insult you with my edit (done in the best intentions), but this might be shocking for you now: [The FAQ](http://stackoverflow.com/faq#editing) - however normally this turns out to work pretty well. – hakre May 03 '12 at 23:09
  • @hakre - Wow that actually is shocking - thanks for pointing that out. I'm all for open source, and I certainly have no problem with others _forking_ my (very carefully and deliberately crafted) answers (which may look like like they need "fixing" to someone for whatever reason). Now that I know I have zero control over the quality of the sources to my own answers, I will certainly not be contributing further, Shit, and I really liked it here. D'oh! Thanks again for the head's up. – ridgerunner May 03 '12 at 23:26
  • 1
    @ridgerunner: If you're not confident with an edit, you can roll-back. Additionally like on wikipedia the whole history is kept with your credits. I've seen you really gardened the answer over time, so it wouldbe a pitty to loose you here. – hakre May 03 '12 at 23:52
  • @ridgerunner Your regex knowledge surpasses all levels. Nice job. ONe question I can seem to modifie to accept this URL: `http://www.youtube.com/watch?feature=v-feature&v=317a815FLWQ` – techAddict82 Jul 20 '12 at 17:09
  • Hello Ridgerunner! Your regular expression has worked like a charm. But, i have one small issue regarding this. I am getting the video id from youtube url in java. I am using the following code http://pastebin.com/ZrPcHZBX which is working fine for all the url formats which u have mentioned above except for the second one. Please help me i dont know where i am doing mistake ... – KK_07k11A0585 Oct 09 '12 at 04:53
  • 1
    Here is one that doesn't worked: http://www.youtube.com/watch?v=E1IPnnttL9k&feature=youtu.be – andrebola Oct 11 '12 at 20:20
  • This is absolutely amazing! Youtube URL's are extremely hard to decipher but this catches 99% of them. I'm trying to generate thumbnails from the ID from twitter and this works pretty well! – user1450909 Dec 03 '12 at 12:45
  • I've used this code for years, it's brilliant but I've just posted a new question regarding 'start' times of a youtube video here: http://stackoverflow.com/questions/15147993/youtube-embedded-video-pregreplace-with-start-timing – Luc Feb 28 '13 at 23:59
  • 1
    This works great, but fails with this (new?) querystring param: feature=youtu.be. Changing [?=&+%\w-]* to [?=&+%\w-\.]* on your "consume remaining url" line does the trick. Thanks! – Mei Gwilym Jun 20 '13 at 13:03
  • @Mei - Thanks for the heads up about the dot-in-query style. Have updated the answer. p.s. Looks like I'm back (couldn't stay away long!) – ridgerunner Aug 24 '13 at 02:52
  • `youtube-nocookie.com` should be added – CRONUS Sep 30 '13 at 10:55
  • @ridgerunner: It's not working for – Gowri Nov 28 '13 at 18:48
  • @gowri - Its not supposed to. The given pattern finds YT URL's that are _not_ already linked. Read the second note for tips on how to modify the pattern to remove this requirement. – ridgerunner Nov 28 '13 at 20:02
  • @CRONUS - Thanks for the nocookie heads up. Just now added this to the pattern. – ridgerunner Nov 30 '13 at 16:45
  • @ridgerunner this is very helpful - thank you. However I just came across an URL which causes the regex to incorrectly catch `desktop_uri` as the video ID: `http://youtube.com/watch?v=cpiU14HTypk&desktop_uri=%2Fwatch%3Fv%3DcpiU14HTypk` – boliva Jan 08 '14 at 16:46
  • Quick and dirty hack to prevent `desktop_uri` matches, replacing the corresponding line: `((?!desktop_uri)[\w-]{11}) # $1: VIDEO_ID is exactly 11 chars.` – boliva Jan 08 '14 at 17:00
  • this kind of link is not handled : http://www.youtube.com/watch?feature=player_detailpage&v=_G2ZHa0Whg0 – David Feb 07 '14 at 03:06
  • @Let Aurn - I just tested it and the regex matches your example YT link just fine (and correctly extracts the YT ID as well). – ridgerunner Feb 07 '14 at 15:14
  • Just as an note for people trying to use this regex outside of PHP; in certain other languages (including C#) the `\w` used in regex does not exactly evaluate to `[A-Za-z0-9_]`; it can include accented characters and alphabets from the current locale. PHP, however, was designed pretty much only with the US locale in mind, so it's a matter of taste whether or not to use it here. If you're worried about false positives with accented characters, make sure you use the expanded notation, and not `\w`. – Shotgun Ninja Mar 31 '15 at 15:57
  • 1
    doesn't work when t is the first paramater: https://www.youtube.com/watch?t=48&v=Opcu8ZJYE6 – Lane Jun 29 '15 at 18:39
  • @Lane - You are correct - this solution does not match your example (`youtube.com/watch?t=48&v=Opcu8ZJYE6`) because it is missing the: `http://`. This solution requires the `http://` scheme to form a absolute URI. – ridgerunner Jul 01 '15 at 04:48
  • Looks like this gives the following error: FATAL ERROR syntax error, unexpected '"][^<>]*> # Either inside a s' (T_CONSTANT_ENCAPSED_STRING) – Henrik Petterson Dec 24 '15 at 16:07
  • Got a link with additional query, that is not matched correctly. `http://www.youtube.com/watch?v=4L1cvtAx4vc&utm_campaign=one%20abcdefghi%20two`. I get `20abcdefghi` instead of `4L1cvtAx4vc`. Any solution to get it right without decoding url or other similar stuff? – CRONUS Jan 25 '16 at 08:23
  • @CRONUS - Fixed it - see above.. Thanks for the erroneous test case! 8^) – ridgerunner Jan 26 '16 at 02:13
  • not working with consecutive links, i.e. `http://youtube.com/v/NLqAF9hrVbY?fs=1&hl=en_UShttp://youtube.com/v/NLqAF9hrVbY?fs=1&hl=en_US` – floydwch Jan 26 '16 at 13:48
  • @floydwch - No, it won't work with two URL stuck together like your example. – ridgerunner Jan 26 '16 at 19:34
  • I searched all answers on how to find youtube video id and this is the best. I also tested it on all examples possible and it works. The only thing that doesn't work is when url doesn't start with http(s), but that is easily fixable, you just need to put `(?:https?:)?(?:\/\/)?` instead `https?:// `, also here is [jsfiddle url](https://jsfiddle.net/kv7hoj9h/8/) with all examples i have tested. – ands Sep 27 '16 at 19:12
  • 1
    Need exception for "videoseries". `https://www.youtube.com/embed/videoseries?list=` Thanks – Maxim Pokrovskii Feb 03 '17 at 14:33
  • 1
    This does not work if URL has the `time_continue` parameter. Example: `https://www.youtube.com/watch?time_continue=4&v=IDSTRING` – Henrik Petterson Mar 20 '18 at 10:55
  • @Henrik Petterson - Your example `IDSTRING` ID string is not exactly 11 characters wide (all IDs must be 11 chars). My testing shows that the regex matches just fine when the ID is 11 (valid=`/[\w-]{11}/`) chars long even when preceded by `time_continue=4&`. Not sure why it is failing for you. – ridgerunner Mar 22 '18 at 11:09
  • @ands - Making the `https?//:` optional is asking for trouble. By doing this, the regex could very well match text that is not a youtube link (and will aslo slow it down). The logic of this regex depends upon requiring the specificity of the URL scheme prefix found in absolute URIs – ridgerunner Mar 22 '18 at 11:17
  • @Maxim Pokrovskii - Yes you are correct. A false ID match occurs with URIs like: `https://www.youtube.com/embed/videoseries?v=youtubeid01` - (the iD is incorrectly matched as `videoseries`). The problem occurs if there is any 'word' having exactly 11 chars preceding the ID. Unfortunately this behavior is not one that is easy to fix considering the simplistic logic used in this regex. – ridgerunner Mar 22 '18 at 11:28
10

Here's a method I once wrote for a project that extracts YouTube and Vimeo video keys:

/**
 *  strip important information out of any video link
 *
 *  @param  string  link to a video on the hosters page
 *  @return mixed  FALSE on failure, array on success
 */
function getHostInfo ($vid_link)
{
  // YouTube get video id
  if (strpos($vid_link, 'youtu'))
  {
    // Regular links
    if (preg_match('/(?<=v\=)([\w\d-_]+)/', $vid_link, $matches))
      return array('host_name' => 'youtube', 'original_key' => $matches[0]); 
    // Ajax hash tag links
    else if (preg_match('§([\d\w-_]+)$§i', $vid_link, $matches))
      return array('host_name' => 'youtube', 'original_key' => $matches[0]);
    else
      return FALSE;
  }
  // Vimeo get video id
  elseif (strpos($vid_link, 'vimeo'))
  {
    if (preg_match('§(?<=/)([\d]+)§', $vid_link, $matches))
      return array('host_name' => 'vimeo', 'original_key' => $matches[0]); 
    else
      return FALSE;
  }
  else
    return FALSE;
}
  1. Find a regex that will extract all links from a text. Google will help you there.
  2. Loop all the links and call getHostInfo() for each
Peter Mortensen
  • 28,342
  • 21
  • 95
  • 123
Christof
  • 3,347
  • 2
  • 32
  • 49
  • 1
    great thanks! slight mod `if(strpos($vid_link, 'youtu'))` will capture the short url `youtu.be` in addition to the common url's. – Chamilyan Oct 08 '11 at 09:53
  • you're welcome. thanks for the update, I edited in the change. on a side note, ridgerunner's regex seems to be the real deal and I recommend using it over my simple thing. cheers – Christof Oct 10 '11 at 06:27
  • exactly what i was looking for. spot on mate! +1 – blackpla9ue Jul 18 '12 at 05:50
8

While ridgerunner's answer is the basis for my answer, his does NOT solve for all urls and I don't believe it is capable of it, due to multiple possible matches of VIDEO_ID in a YouTube URL. My regex includes his aggressive approach as a last resort, but attempts all common matchings first, vastly reducing the possibility of a wrong match later in the URL.

This regex:

/https?:\/\/(?:[0-9A-Z-]+\.)?(?:youtu\.be\/|youtube\.com(?:\/embed\/|\/v\/|\/watch\?v=|\/ytscreeningroom\?v=|\/feeds\/api\/videos\/|\/user\S*[^\w\-\s]|\S*[^\w\-\s]))([\w\-]{11})[?=&+%\w-]*/ig;

Handles all of the cases originally referenced in ridgerunners examples, plus any url that might happen to have an 11 character sequence later in the url. ie:

http://www.youtube.com/watch?v=GUEZCxBcM78&feature=pyv&feature=pyv&ad=10059374899&kw=%2Bwingsuit

Here is a working sample that tests all of the sample YouTube urls:

http://jsfiddle.net/DJSwc/5/

ezwrighter
  • 869
  • 8
  • 16
2

Use:

<?php

    // The YouTube URL string

    $youtube_url='http://www.youtube.com/watch?v=8VtUYvwktFQ';

    // Use regex to get the video ID

    $regex='#(?<=v=)[a-zA-Z0-9-]+(?=&)|(?<=[0-9]/)[^&\n]+|(?<=v=)[^&\n]+#';

    preg_match($regex, $youtube_url, $id);

    // Plug that into our HTML
?>
Peter Mortensen
  • 28,342
  • 21
  • 95
  • 123
Noor Khan
  • 542
  • 1
  • 5
  • 12
2

Okay, I made a function of my own. But I believe it's pretty inefficient. Any improvements are welcome:

function get_youtube_videos($string) {

    $ids = array();

    // Find all URLs
    preg_match_all('/(http|https)\:\/\/[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,3}(\/\S*)?/', $string, $links);

    foreach ($links[0] as $link) {
        if (preg_match('~youtube\.com~', $link)) {
            if (preg_match('/[^=]+=([^?]+)/', $link, $id)) {
                $ids[] = $id[1];
            }
        }
    }
    return $ids;
}
Peter Mortensen
  • 28,342
  • 21
  • 95
  • 123
n00b
  • 14,958
  • 19
  • 52
  • 71
  • If you are looking only for links from youtube.com why do you want to build at first a list with all links? And I think its not necessary to use 3 different regexes. – stema Apr 29 '11 at 10:13
2

Try

[^\s]*youtube\.com[^\s]*?v=([-\w]+)[^\s]*

You will find the video IDs' in the first capturing group. What I don't know is what is a valid Video ID? At the moment I check for v= and capture all -A-Za-z0-9_.

I checked it online here on rubular with your sample string.

stema
  • 80,307
  • 18
  • 92
  • 121
1

I tried a simple expression to get only the videoid:

[?&]v=([^&#]*)

Check it working online here at phpliveregex.

Peter Mortensen
  • 28,342
  • 21
  • 95
  • 123
B L Praveen
  • 1,310
  • 2
  • 18
  • 39
1

The original poster asked "I would like to parse it and find all YouTube video URLs and their ids." I switched the most popular answer above to a preg_match and returned the video id and URL.

Get YouTube URL and ID from post:

$match[0] = Full URL
$match[1] = video ID

function get_youtube_id($input) {
    $input = preg_match('~https?://(?:[0-9A-Z-]+\.)?(?:youtu\.be/|youtube(?:-nocookie)?\.com\S*[^\w\s-])([\w-]{11})(?=[^\w-]|$)(?![?=&+%\w.-]*(?:[\'"][^<>]*>|</a>))[?=&+%\w.-]*~ix',
                        $input, $match);
    return $match;
}
Peter Mortensen
  • 28,342
  • 21
  • 95
  • 123
Lee Woodman
  • 1,279
  • 2
  • 16
  • 29
0

Find a YouTube link easily from a string:

function my_url_search($se_action_data)
{
    $regex = '/https?\:\/\/[^\" ]+/i';
    preg_match_all($regex, $se_action_data, $matches);
    $get_url=array_reverse($matches[0]);
    return array_unique($get_url);
}
echo my_url_search($se_action_data)
Peter Mortensen
  • 28,342
  • 21
  • 95
  • 123
0
String urlid="" ;
String  url="http://www.youtube.com/watch?v=0zM4nApSvMg#t=0m10s";
Pattern pattern =Pattern.compile("(?:http|https|)(?::\\/\\/|)(?:www.|)(?:youtu\\.be\\/|youtube\\.com(?:\\/embed\\/|\\/v\\/|\\/watch\\?v=|\\/ytscreeningroom\\?v=|\\/feeds\\/api\\/videos\\/|\\/user\\\\S*[^\\w\\-\\s]|\\S*[^\\w\\-\\s]))([\\w\\-\\_]{11})[a-z0-9;:@#?&%=+\\/\\$_.-]*");
Matcher result = pattern.matcher(url);
    if (result.find())
    {
         urlid=result.group(1);

    }

This code in java works absolutely fine for all youtube urls at present.