1

I'm using the following function to get video IDs from YouTube URLs.

static String getVideoIdFromUrl(String url) {
    String regex = "http(?:s)?://(?:www\\.)?youtu(?:\\.be/|be\\.com/(?:watch\\?v=|v/\u200C\u200B|embed/|user/(?:[\\w#\u200C\u200B]+/)+))([^&#?\\n]+)";
    String id = null;
    Pattern pattern = Pattern.compile(regex, Pattern.CASE_INSENSITIVE);
    Matcher matcher = pattern.matcher(url);
    if (matcher.matches()) {
        id = matcher.group(1);
    }

    return id;
}

This works for getting the video ID for almost all formats. However, it returns null when provided with URLs with a timestamp.

http://www.youtube.com/watch?v=0zM4nApSvMg#t=0m10s
https://www.youtube.com/watch?v=Br5xdYVbcWw&t=50

How do I write a function that returns both the video ID and the timestamp?

Zoe
  • 27,060
  • 21
  • 118
  • 148
Fragmental
  • 75
  • 8
  • 1
    Maybe [**`v=([^#&]+)`**](https://regex101.com/r/Zv6lb1/1/) ? I'm not a `Java` guy but I'm pretty sure there's a function for splitting url strings into theirs respective parts. – Jan Jan 29 '18 at 21:01
  • Not exactly sure either, but I think you need a group around the tlds: `(?:\.be/|be\.com/)` – ctwheels Jan 29 '18 at 21:02

2 Answers2

1

You can't return multiple variables in java, however you could load both the id and time into a HashMap and return that. It might look something like the following:

static Map<String,String> getVideoIdFromUrl(String url) {

  // PATTERN
  String regex = "v=([^#&\n\r]+)|t=([^#&\n\r]+)";

  // INIT RETURN DATA
  String id = "";
  String time = "";

  // RUN REGEX
  Pattern pattern = Pattern.compile(regex, Pattern.CASE_INSENSITIVE);
  Matcher matcher = pattern.matcher(url);

  // CHECK 
  while (matcher.find()) {

      // SET ID
      if(matcher.group(1) != null)
          id = matcher.group(1);

      // SET TIME
      if(matcher.group(2) != null)
          time = matcher.group(2);

  }

  // BUILD RETURN DATA
  Map<String,String> returnData = new HashMap<String, String>();
  returnData.put("id", id);
  returnData.put("time", time);

  // RETURN
  return returnData;

}

I ran this function with the following output:

// RUN FUNCTION
Map<String,String> returnMap = myObject.getVideoIdFromUrl("http://www.youtube.com/watch?v=0zM4nApSvMg#t=0m10s");

// PRINT OUTPUT
System.out.println(returnMap);

// PRINTS:
// {id=0zM4nApSvMg, time=0m10s}
Tony
  • 2,890
  • 1
  • 24
  • 35
  • The regex pattern you provided doesn't work with any YouTube URL. There are some different ones in [this thread](https://stackoverflow.com/questions/3717115/regular-expression-for-youtube-links?rq=1) but what I'm looking for is something that returns the timestamp with matcher.group(2). – Fragmental Jan 30 '18 at 18:26
  • @Fragmental The function should be returning both the video id and time parameter. Which URLs is the query not working with, and what are you expecting it to return? – Tony Jan 30 '18 at 21:36
  • It's working well now that you've updated it. Thanks! – Fragmental Jan 31 '18 at 22:19
1
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 matches almost all youtube urls.

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