如何从 postgresql 中的 URL 中提取相对路径

How to extract relative path from a URL in postgresql

我有一个 URL,我想提取主机名以外的所有内容。如何实现?例如:

我的字符串:https://title001-stg.azuge.net/enstgormedia/media/802/titles/123.srt

我的要求:media/802/titles/123.srt

你可以利用PG的REGEXP_REPLACE

例如,如果您有 table 和如下值

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');

您可以使用

提取您想要的部分
select REGEXP_REPLACE(url, 'https://[a-zA-Z0-9\-]+.[a-zA-Z0-9\-]+.[a-z]+\/[a-z]+\/','') from testexample;

正则表达式 https://[a-zA-Z0-9\-]+.[a-zA-Z0-9\-]+.[a-z]+\/[a-z]+\/ 模仿您示例中的 http://a.b.net/folder/ 设置