I got this code to get the youtube id from the links like www.youtube.com/watch?v=xxxxxxx
URL youtubeURL = new URL(link);
youtubeURL.getQuery();
This regex would do the trick:
(?<=videos\/|v=)([\w-]+)
This means that we're first looking for video/ or v= then captures all the following characters that can be in word (letters, digits, and underscores) and hyphens.
Example in java:
public static void main(String[] args) {
String link = "http://gdata.youtube.com/feeds/api/videos/xTmi7zzUa-M&whatever";
String pattern = "(?:videos\\/|v=)([\\w-]+)";
Pattern compiledPattern = Pattern.compile(pattern);
Matcher matcher = compiledPattern.matcher(link);
if(matcher.find()){
System.out.println(matcher.group());
}
}
Output:
xTmi7zzUa-M