如何在postgresql中替换URL中的值

Posted

技术标签:

【中文标题】如何在postgresql中替换URL中的值【英文标题】:How to replace value in URL in postgresql 【发布时间】:2020-04-21 22:20:08 【问题描述】:

我正在尝试将https://place.mycompany.com/analyst/5e0361f5af70f40044112148/dbt 中的5e0361f5af70f400441148 替换为id。最后应该收到 https://place.mycompany.com/analyst/id/dbt

我使用regexp_replace('https://place.mycompany.com/analyst/5e0361f5af70f400441148/dbt','[[:digit:]]','','g'),但我也不知道如何替换字母

【问题讨论】:

【参考方案1】:

如果该 ID 始终使用小写 a-f,则可以执行以下操作:

SELECT regexp_replace('https://place.mycompany.com/analyst/5e0361f5af70f400441148/dbt' , '/[0-9,a-f]+/','/id/');

否则添加 A-F 范围:'/[0-9,a-f,A-F]+/'

如果您的 url 的其他部分仅包含 [0-9,a-f] 字符,这将停止。

如果您的输入包含多个 id,请添加 'g' 修饰符,如下所示:

SELECT regexp_replace('https://place.mycompany.com/analyst/5e0361f5af70f40044112148/dbt/5e036445af70f40b1012b48f/top-chart', '/[0-9,a-f]+/','/id/','g');

【讨论】:

这部分\/做什么?如果我有 2 个 id 的 place.mycompany.com/analyst/5e0361f5af70f40044112148/dbt/… 其实不用转义,我改一下。 添加了 'g' 修饰符,以防您的输入中有多个 id。 知道了。谢谢! just found: 'g' 指示函数删除所有,而不仅仅是第一个。

以上是关于如何在postgresql中替换URL中的值的主要内容,如果未能解决你的问题,请参考以下文章