V
V
Vladislav2020-06-10 09:11:54
Regular Expressions
Vladislav, 2020-06-10 09:11:54

Need regular expression evaluation and criticism?

There are different types of links to YouTube channels:

http://www.youtube.com/user/TheJuicyJVEVO
https://www.youtube.com/channel/UC4SusBgD3b1G5cCh_gjA81g
http://www.youtube.com/user/fabrikaband?gl=RU&hl=ru
https://www.youtube.com/channel/UCd2R33j8_ZtNtP2HmXNqAGA/featured
https://www.youtube.com/channel/UCd2R33j8_ZtNtP2HmXNqAGA/videos?a=b&b=c


I wrote a regular expression to pull out the channel ID in PHP:
function getGetIdChannel($link)
    {
        preg_match('~(channel\/|user\/)(?P<unslash>[\w_-]+)|\/(channel\/|user\/)(?P<slash>[\w_-]+)\/~ui', $link, $matches);

        if (isset($matches['unslash']) && !empty($matches['unslash'])) {
            return $matches['unslash'];
        } elseif (isset($matches['slash']) && !empty($matches['slash'])) {
            return $matches['slash'];
        }
    }

Everything works as if and finds. I would like someone to tell me if the expression is too big?
Maybe there are other options?

And of course 1 question.
There are also such channels:
https://www.youtube.com/duranduran
https://www.youtube.com/r3hab
How to fit everything into one regular season and also put the third type?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
T
Timur Pokrovsky, 2020-06-10
@cr1gger

https://regex101.com/r/acsBMf/3 ?

D
Dr. Bacon, 2020-06-10
@bacon

Why are there regular expressions when you can get by with a simple partition by the separator.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question