Skip to content

Instantly share code, notes, and snippets.

@bapspatil
Created November 5, 2017 09:14
Show Gist options
  • Save bapspatil/e21193ffbb76b48f958bfbcce4e3c345 to your computer and use it in GitHub Desktop.
Save bapspatil/e21193ffbb76b48f958bfbcce4e3c345 to your computer and use it in GitHub Desktop.
Get the video ID for a YouTube video from its YouTube URL
/*
** Created by bapspatil
*/
private static String extractVideoId(String ytUrl) {
String vId = null;
Pattern pattern = Pattern.compile("(?<=watch\\\\?v=|/videos/|embed\\\\/|youtu.be\\\\/|\\\\/v\\\\/|\\\\/e\\\\/|watch\\\\?v%3D|watch\\\\?feature=player_embedded&v=|%2Fvideos%2F|embed%\\u200C\\u200B2F|youtu.be%2F|%2Fv%2F)[^#\\\\&\\\\?\\\\n]*");
Matcher matcher = pattern.matcher(ytUrl);
if (matcher.matches()){
vId = matcher.group(1);
}
return vId;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment