How we can find domain name using MySQL and regular expression

前端 未结 5 1850
花落未央
花落未央 2020-12-07 02:08

i am having some list of domains in the DB,like

http://www.masn.com/index.html
http://www.123musiq.com/index.html etc

w

5条回答
  •  萌比男神i
    2020-12-07 02:31

    In MySQL, regular expressions can match but not return substrings.

    You can use SUBSTRING_INDEX:

    SELECT  SUBSTRING_INDEX('www.example.com', '/', 1)
    

    , however, it's not protocol prefix safe.

    If you are using a mix of prefixed and unprefixed URL's, use this:

    SELECT  url RLIKE '^http://',
            CASE
            WHEN url RLIKE '^http://' THEN
                    SUBSTRING_INDEX(SUBSTRING_INDEX(url, '/', 3), '/', -1)
            ELSE
                    SUBSTRING_INDEX(url, '/', 1)
            END
    FROM    (
            SELECT   'www.example.com/test/test' AS url
            UNION ALL
            SELECT   'http://www.example.com/test'
            ) q
    

提交回复
热议问题