I’m looking for assistance with a regex expression that can validate if a URL belongs to Twitch and extract the username from it. Currently, I’m trying this code:
preg_match("/^[(http|https):\/\/www.twitch.tv\/]+((?:[a-zA-Z0-9][\w]{3,24}))$/", $url, $result);
The issue is strange - it works well for many usernames but fails when the username begins with certain letters like w, t, p, s, h, c, v, or b.
For instance, with https://www.twitch.tv/GoodGamer
, it returns correctly:
- 0 =>
https://www.twitch.tv/GoodGamer
- 1 => GoodGamer
However, when using https://www.twitch.tv/awesomePlayer
, it results in:
- 0 =>
https://www.twitch.tv/awesomePlayer
- 1 => wesomePlayer
It seems to remove the first character in some cases. I use [a-zA-Z0-9] at the beginning since a Twitch username cannot start with an underscore. Can anyone help me identify what’s wrong with this regex pattern?