In MySQL
, regular expressions can match but not return substrings.
You can use SUBSTRING_INDEX
:
SELECT SUBSTRING_INDEX('www.example.com', "https://stackoverflow.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, "https://stackoverflow.com/", 3), "https://stackoverflow.com/", -1)
ELSE
SUBSTRING_INDEX(url, "https://stackoverflow.com/", 1)
END
FROM (
SELECT 'www.example.com/test/test' AS url
UNION ALL
SELECT 'http://www.example.com/test'
) q