I stumbled upon this JavaScript regex that extracts IDs from the Youtube URLs provided below:
/(youtu(?:\.be|be\.com)\/(?:.*v(?:\/|=)|(?:.*\/)?)([\w'-]+))/i
Youtube URLs tested on:
http://www.youtube.com/user/Scobleizer#p/u/1/1p3vcRhsYGo
http://www.youtube.com/watch?v=cKZDdG9FTKY&feature=channel
... (other YouTube URLs listed)
How can I adapt this regex for use in Java? And is it possible to modify it to extract IDs from gdata URLs as well? For example,
https://gdata.youtube.com/feeds/api/users/Test/?alt=json&v=2
Update: This is where I intend to implement the Regex function.
public static String getIDFromYoutubeURL(String ytURL ) {
if(ytURL.startsWith("https://gdata")) { // This is my temporary workaround,
ytURL = ytURL.replace("v=\\d", ""); // I believe the Regex should handle this.
}
String pattern = "(?i)(https://gdata\\.)?(youtu(?:\\.be|be\\.com)/(?:.*v(?:/|=)|(?:.*/)?)([\\w'-]+))";
Pattern compiledPattern = Pattern.compile(pattern);
Matcher matcher = compiledPattern.matcher(ytURL);
if(matcher.find()){
return matcher.group(3);
}
return null;
}
The current implementation works for most YouTube URLs and also for
https://gdata.youtube.com/feeds/api/users/Test/?id=c
. However, it fails when dealing with Gdata URLs that contain version parameters, like v=2 (https://gdata.youtube.com/feeds/api/users/Test/?id=c&v=2
). In such cases, it returns 2 instead of Test as the ID. How can I enhance it to retrieve Test instead of 2 as the ID in Gdata URLs?
Thanks.