How to extract relative path from a URL in postgresql

gauri

I have an URL and I want to extract everything other than host name. How can this be achieved? For eg:

My string: https://title001-stg.azuge.net/enstgormedia/media/802/titles/123.srt

My requirement: media/802/titles/123.srt

Ftisiot

you can make use of PG's REGEXP_REPLACE

For example if you have table and values like the below

create table testexample (url varchar);

insert into testexample values ('https://title001-stg.azuge.net/enstgormedia/media/802/titles/123.srt');
insert into testexample values ('https://title002-stg.azuge.net/enstgormedia/media/804/titles/123.srt');

You could extract the part you want with

select REGEXP_REPLACE(url, 'https://[a-zA-Z0-9\-]+.[a-zA-Z0-9\-]+.[a-z]+\/[a-z]+\/','') from testexample;

The regular espression https://[a-zA-Z0-9\-]+.[a-zA-Z0-9\-]+.[a-z]+\/[a-z]+\/ mimics the http://a.b.net/folder/ setup in your example

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related