androidregexyoutubeyoutube-channels

Extract youtube channel id from channel url - android


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

I need only UCAoMPWcQKA_9Af5YhWdrZgw (Channel ID)


Solution

  • 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.