Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How we can find domain name using MySQL and regular expression

Tags:

mysql

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

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

what i need as out put is

http://www.masn.com
http://www.123musiq.com

how can i do that in regular expression???

like image 287
Alex Mathew Avatar asked Aug 19 '10 11:08

Alex Mathew


3 Answers

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
like image 177
Quassnoi Avatar answered Sep 22 '22 11:09

Quassnoi


use substring_index

http://dev.mysql.com/doc/refman/5.1/en/string-functions.html#function_substring-index

like

SELECT  SUBSTRING_INDEX(urlfield, '/', 1) from mytable
like image 44
Haim Evgi Avatar answered Sep 21 '22 11:09

Haim Evgi


SELECT SUBSTRING_INDEX(SUBSTRING_INDEX('http://www.domain.com/', '://', -1),'/', 1);

Result: www.domain.com

SELECT SUBSTRING_INDEX(SUBSTRING_INDEX(SUBSTRING_INDEX('http://www.domain.com/', '://', -1),'/',1),'www.', -1);

Result: domain.com

like image 37
Alexander K. Avatar answered Sep 20 '22 11:09

Alexander K.