regexp_replace以使用postgresql

时间:2019-05-10 20:42:41

标签: regex postgresql

我想在postgres中获得友好的URL。示例:

我希望url = https://this-is-the-domain-i-want/?but_not_this#stuff%stuff和url = https://this-is-the-domain-i-want/this-too/?but_not_this#stuff%stuff分别返回https://this-is-the-domain-i-want/https://this-is-the-domain-i-want/this-too/

我可以为此成功使用嵌套的regexp_replace:

  

REGEXP_REPLACE(REGEXP_REPLACE(REGEXP_REPLACE(url,'\?(。)',''),'\%(。)',''),'#(。*)', '')

但是我希望有一种更清洁的方法(不需要多次遍历该URL。

我知道regex有一个| or,但是我尝试过REGEXP_REPLACE(url, '\?(.*)|\%(.*)|\#(.*)|\_(.*)', '')却没有成功。

1 个答案:

答案 0 :(得分:0)

因此,基本上,您只想删除查询字符串和片段,即.. <build> <sourceDirectory>${project.basedir}/src</sourceDirectory> <plugins> <plugin> .... .. 之后的所有内容,包括??的类似物。

那将是:

#

我不会删除regexp_replace(url, '[\?#].*', ''); _,因为它们在标识主要资源时可能具有含义(%只是合法字符,_引入了URL编码字符)。