How to get video id from YouTube regex url in Java

Porting from javascript response to Java JavaScript REGEX version: How to get YouTube video id from URL?

+7
java regex youtube
source share
8 answers

I realized that this question was not here for Java, if you need to do it in Java here in a way

public static String extractYTId(String ytUrl) { String vId = null; Pattern pattern = Pattern.compile( "^https?://.*(?:youtu.be/|v/|u/\\w/|embed/|watch?v=)([^#&?]*).*$", Pattern.CASE_INSENSITIVE); Matcher matcher = pattern.matcher(ytUrl); if (matcher.matches()){ vId = matcher.group(1); } return vId; } 

Works for URLs like (also for https://... )

 http://www.youtube.com/watch?v=0zM4nApSvMg&feature=feedrec_grec_index http://www.youtube.com/user/SomeUser#p/a/u/1/QDK8U-VIH_o http://www.youtube.com/v/0zM4nApSvMg?fs=1&hl=en_US&rel=0 http://www.youtube.com/watch?v=0zM4nApSvMg#t=0m10s http://www.youtube.com/embed/0zM4nApSvMg?rel=0 http://www.youtube.com/watch?v=0zM4nApSvMg http://youtu.be/0zM4nApSvMg 
+30
source share

The previous answers do not work for me .. This is the correct method. It works with links www.youtube.com and youtu.be.

 private String getYouTubeId (String youTubeUrl) { String pattern = "(?<=youtu.be/|watch\\?v=|/videos/|embed\\/)[^#\\&\\?]*"; Pattern compiledPattern = Pattern.compile(pattern); Matcher matcher = compiledPattern.matcher(youTubeUrl); if(matcher.find()){ return matcher.group(); } else { return "error"; } } 
+10
source share

For me, for links to links that you sent, this regular expression looks more accurate (capturing an identifier for group 1):

 http://(?:www\.)?youtu(?:\.be/|be\.com/(?:watch\?v=|v/|embed/|user/(?:[\w#]+/)+))([^&#?\n]+) 

In the demo, see capture groups in the lower right pane. However, for the second match I’m not quite sure that this is the correct identifier, since it looks different than other patterns - if necessary, it will be changed.

In the code, something like:

 Pattern regex = Pattern.compile("http://(?:www\\.)?youtu(?:\\.be/|be\\.com/(?:watch\\?v=|v/|embed/|user/(?:[\\w#]+/)+))([^&#?\n]+)"); Matcher regexMatcher = regex.matcher(subjectString); if (regexMatcher.find()) { VideoID = regexMatcher.group(1); } 
+3
source share

Tried others but failed in my case - adjusted regex to match my urls

 public static String extractYoutubeVideoId(String ytUrl) { String vId = null; String pattern = "(?<=watch\\?v=|/videos/|embed\\/)[^#\\&\\?]*"; Pattern compiledPattern = Pattern.compile(pattern); Matcher matcher = compiledPattern.matcher(ytUrl); if(matcher.find()){ vId= matcher.group(); } return vId; } 

This file works below

 http://www.youtube.com/embed/Woq5iX9XQhA?html5=1 http://www.youtube.com/watch?v=384IUU43bfQ http://gdata.youtube.com/feeds/api/videos/xTmi7zzUa-M&whatever https://www.youtube.com/watch?v=C7RVaSEMXNk 

Hope this helps someone. Thanks

+2
source share

This is very similar to the link posted in your question.

 String url = "https://www.youtube.com/watch?v=wZZ7oFKsKzY"; if(url.indexOf("v=") == -1) //there no video id return ""; String id = url.split("v=")[1]; //everything before the first 'v=' will be the first element, ie [0] int end_of_id = id_to_end.indexOf("&"); //if there are other parameters in the url, get only the id value if(end_index != -1) id = url.substring(0, end_of_id); return id; 
0
source share

This will work for me and just ..

 public static String getVideoId(@NonNull String videoUrl) { String reg = "(?:youtube(?:-nocookie)?\\.com\\/(?:[^\\/\\n\\s]+\\/\\S+\\/|(?:v|e(?:mbed)?)\\/|\\S*?[?&]v=)|youtu\\.be\\/)([a-zA-Z0-9_-]{11})"; Pattern pattern = Pattern.compile(reg, Pattern.CASE_INSENSITIVE); Matcher matcher = pattern.matcher(videoUrl); if (matcher.find()) return matcher.group(1); return null; } 
0
source share

Just pass the link to YouTube in the bottom function, it will detect the whole type of youtube url

  public static String getYoutubeID(String youtubeUrl) { if (TextUtils.isEmpty(youtubeUrl)) { return ""; } String video_id = ""; String expression = "^.*((youtu.be" + "\\/)" + "|(v\\/)|(\\/u\\/w\\/)|(embed\\/)|(watch\\?))\\??v?=?([^#\\&\\?]*).*"; // var regExp = /^.*((youtu.be\/)|(v\/)|(\/u\/\w\/)|(embed\/)|(watch\?))\??v?=?([^#\&\?]*).*/; CharSequence input = youtubeUrl; Pattern pattern = Pattern.compile(expression, Pattern.CASE_INSENSITIVE); Matcher matcher = pattern.matcher(input); if (matcher.matches()) { String groupIndex1 = matcher.group(7); if (groupIndex1 != null && groupIndex1.length() == 11) video_id = groupIndex1; } if (TextUtils.isEmpty(video_id)) { if (youtubeUrl.contains("youtu.be/") ) { String spl = youtubeUrl.split("youtu.be/")[1]; if (spl.contains("\\?")) { video_id = spl.split("\\?")[0]; }else { video_id =spl; } } } return video_id; } 
0
source share

I know this topic is old, but I get more cases to get Youtube id, this is my regex

 http(?:s)?:\/\/(?:m.)?(?:www\.)?youtu(?:\.be\/|be\.com\/(?:watch\?(?:feature=youtu.be\&)?v=|v\/|embed\/|user\/(?:[\w#]+\/)+))([^&#?\n]+) 

This will work for

 http://www.youtube.com/watch?v=0zM4nApSvMg&feature=feedrec_grec_index http://www.youtube.com/user/SomeUser#p/a/u/1/QDK8U-VIH_o http://www.youtube.com/v/0zM4nApSvMg?fs=1&amp;hl=en_US&amp;rel=0 http://www.youtube.com/watch?v=0zM4nApSvMg#t=0m10s http://www.youtube.com/embed/0zM4nApSvMg?rel=0 http://www.youtube.com/watch?v=0zM4nApSvMg http://youtu.be/0zM4nApSvMg https://www.youtube.com/watch?v=nBuae6ilH24 https://www.youtube.com/watch?v=pJegNopBLL8 http://www.youtube.com/watch?v=0zM4nApSvMg#t=0m10s https://www.youtube.com/watch?v=0zM4nApSvMg&feature=youtu.be https://www.youtube.com/watch?v=_5BTo2oZ0SE https://m.youtube.com/watch?feature=youtu.be&v=_5BTo2oZ0SE https://m.youtube.com/watch?v=_5BTo2oZ0SE https://www.youtube.com/watch?feature=youtu.be&v=_5BTo2oZ0SE&app=desktop https://www.youtube.com/watch?v=nBuae6ilH24 https://www.youtube.com/watch?v=eLlxrBmD3H4 

Note. Using the regex matching group (1) to get the identifier

-one
source share

All Articles