How to extract Twitch username from a URL using regex

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?

Your character class syntax is the problem. When you write [(http|https):\/\/www.twitch.tv\/], those square brackets create a character class that matches any single character from that set - not the literal string you want. So characters like ‘h’, ‘t’, ‘p’, ‘s’, ‘w’, ‘c’, ‘v’ get consumed before your username capture group even runs. Try this instead: ^https?:\/\/(?:www\.)?twitch\.tv\/([a-zA-Z0-9]\w{3,24})$. The ?: makes it non-capturing, and https? handles both protocols correctly.

your character class [(http|https):\/\/www.twitch.tv\/] is the problem - it’s matching individual characters instead of the full string. use ^https?:\/\/(?:www\.)?twitch\.tv\/([a-zA-Z0-9]\w{3,24})$ instead. that’ll fix the missing first letter issue.