英文:
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');
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论