如何在PostgreSQL中替换URL中的值

huangapple go评论67阅读模式
英文:

How to replace value in URL in postgresql

问题

I am trying to replace 5e0361f5af70f400441148 in https://place.mycompany.com/analyst/5e0361f5af70f40044112148/dbt to id. In the end should receive https://place.mycompany.com/analyst/id/dbt.

I use regexp_replace('https://place.mycompany.com/analyst/5e0361f5af70f400441148/dbt','[[:digit:]]','','g') but I do not know how to replace alphabet also.

英文:

I am trying to replace 5e0361f5af70f400441148 in https://place.mycompany.com/analyst/5e0361f5af70f40044112148/dbt to id. In the end should receive
https://place.mycompany.com/analyst/id/dbt

I use regexp_replace('https://place.mycompany.com/analyst/5e0361f5af70f400441148/dbt','[[:digit:]]','','g') but i do not know how to replace alphabet also

答案1

得分: 0

如果该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');
英文:

If that Id is always using lowercase a-f, the following will do:

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

Otherwise add the A-F range: '/[0-9,a-f,A-F]+/'

This will break if other parts of your url contain [0-9,a-f] characters only.

If your input contains more than one id, add a 'g' modifier like so:

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

huangapple
  • 本文由 发表于 2020年1月3日 18:41:02
  • 转载请务必保留本文链接:https://go.coder-hub.com/59577098.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定