Example URL : https://www.youtube.com/channel/UCAoMPWcQKA_9Af5YhWdrZgw
I need only UCAoMPWcQKA_9Af5YhWdrZgw (Channel ID)
Example URL : https://www.youtube.com/channel/UCAoMPWcQKA_9Af5YhWdrZgw
I need only UCAoMPWcQKA_9Af5YhWdrZgw (Channel ID)
Subhendu Mondal decision crashes on links like this.
// args after id
https://www.youtube.com/channel/UCAoMPWcQKA_9Af5YhWdrZgw?view_as=subscriber
//minus sign in id
https://www.youtube.com/channel/UCQ18THOcZ8nIP-A3ZCqqZwA
so here is improved decision, also protocol group was removed from match result
^(?:http|https):\/\/[a-zA-Z-]*\.{0,1}[a-zA-Z-]{3,}\.[a-z]{2,}\/channel\/([a-zA-Z0-9_\-]{3,24})
var str = "https://www.youtube.com/channel/UCAoMPWcQKA_9Af5YhWdrZgw";
var pattern = /^(?:(http|https):\/\/[a-zA-Z-]*\.{0,1}[a-zA-Z-]{3,}\.[a-z]{2,})\/channel\/([a-zA-Z0-9_]{3,})$/;
var match = str.match(pattern);
console.log(match[1]);
UPDATE:
Here is the shortest way:
youtube.com\/channel\/([^#\&\?]*).*
Parse the string into a URI:
Uri uri = Uri.parse("https://www.youtube.com/channel/UCAoMPWcQKA_9Af5YhWdrZgw")
String id = uri.lastPathSegment() //UCAoMPWcQKA_9Af5YhWdrZgw
This is the pattern you can use to capture the channel id and this will validate the url also
^(?:(http|https):\/\/[a-zA-Z-]*\.{0,1}[a-zA-Z-]{3,}\.[a-z]{2,})\/channel\/([a-zA-Z0-9_]{3,})$
I have no idea how to execute regex in android but sharing the regex url, you can check from here https://regex101.com/r/9sjMPp/1
Or a javascript code to perform
var str = "https://www.youtube.com/channel/UCAoMPWcQKA_9Af5YhWdrZgw";
var pattern = /^(?:(http|https):\/\/[a-zA-Z-]*\.{0,1}[a-zA-Z-]{3,}\.[a-z]{2,})\/channel\/([a-zA-Z0-9_]{3,})$/;
var matchs = str.match(pattern);
console.log(matchs[2]);
// output is UCAoMPWcQKA_9Af5YhWdrZgw
Hope you get the idea.
You can use this regex for retrieving video ids from different types of youtube url.
String pattern = "(?<=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]*";
Then generate a Pattern instance using the above regex, from which you can get a Matcher instance by using Pattern.matcher(videoLink)
.
Now the Matcher.find()
will give you a boolean on whether the above regex occurs in the given video link or not.
Depending on that you can use Matcher.group()
, which will give you back the video id.