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();
basically this will get me the id easily v=xxxxxxxx
but I noticed sometime youtube links will be like this
http://gdata.youtube.com/feeds/api/videos/xxxxxx
I am getting the links from a feed so do I need to build a regex for that or theres a parser to get that for me ?
Without knowing the complete specification for all the possible YouTube URLs, this seems to work for the examples you provided:
... matches
PjDw3azfZWI
from either of these URLS:You would need a little more to get that particular info if you did not know that these were from youtube, though that's a pretty quick check
Keep in mind that if you are trying to use only the result of the
getQuery()
method, it will not be possible to extract the result from thehttp://gdata.youtube.com/feeds/api/videos/PjDw3azfZWI
URL, as this URL does not have a query part to it...Java Example:
This doesn't use a regex but should still do the job.
Tried the other ones but failed in my case - adjusted the regex to fit for my urls
This one works for: (you could also implement a security check youtubeid length = 11 )
That pattern worked for me:
source: Regular expression for youtube links
This regex would do the trick:
This means that we're first looking for
video/
orv=
then captures all the following characters that can be in word (letters, digits, and underscores) and hyphens.Example in java:
Output: