0

Example URL : https://www.youtube.com/channel/UCAoMPWcQKA_9Af5YhWdrZgw

I need only UCAoMPWcQKA_9Af5YhWdrZgw (Channel ID)

Android Guy
  • 573
  • 1
  • 8
  • 18

4 Answers4

3

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\/([^#\&\?]*).*
user2993505
  • 83
  • 2
  • 9
2

Parse the string into a URI:

    Uri uri = Uri.parse("https://www.youtube.com/channel/UCAoMPWcQKA_9Af5YhWdrZgw")
    String id = uri.lastPathSegment() //UCAoMPWcQKA_9Af5YhWdrZgw
Udit
  • 1,037
  • 6
  • 11
1

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.

Subhendu Mondal
  • 121
  • 1
  • 9
0

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.

Refer Here

sissyphus_69
  • 350
  • 5
  • 18