Sql where子句中的Postgres正则表达式

Sql where子句中的Postgres正则表达式,sql,regex,postgresql,Sql,Regex,Postgresql,postgres中是否有如下示例所示的匹配解决方案: “测试”表条目: id | url ----+------------------------------------------------------------- 1 | /services/system 2 | /services/system/{uuid}/group 3 | /services/system/

postgres中是否有如下示例所示的匹配解决方案:

“测试”表条目:

id |                            url                             
----+-------------------------------------------------------------
 1  | /services/system
 2  | /services/system/{uuid}/group
 3  | /services/system/{uuid}/group/{uuid}/port
我想将以下输入字符串与表中存在的url列相匹配:

1. /services/system/1/group          --> should match row 3
2. /services/system/1/group/2/port   --> should match row 3
3. /services/system/1/group          --> should match row 2
4. /services/system/1/group/2        --> should not match
5. /services/system/1                --> should not match
我尝试以下查询来匹配第三行的匹配项,但不起作用:

select * from test where regexp_replace(url, '{uuid}', '*', 'g') ~ '/services/system/1/group/1/port'

有什么解决方案吗?

考虑到您的问题场景和评论,您可以使用以下查询:

从测试中选择*
其中'/services/system/1/group'~concat('^',replace(url,{uuid}','[^/]+'),'$'))
在这里,它将检查除代替
{uuid}

编辑 如果您只需要字母数字,则应使用以下方法:

从测试中选择*
其中'/services/system/1/group'~concat('^',replace(url,{uuid}','[\w]+'),'$'))

*
不是正则表达式中的通配符。您需要使用
+
[0-9]+
来只匹配数字OK,但“+”也不起作用。uuid占位符也不一定是数字/1/和/2/只是用来简化这个例子。我想你应该这样试试<代码>从测试中选择*,其中regexp_replace('/services/system/1/group/1/port','[0-9]+','{uuid}','g')=url@AkhileshMishra正如我所说,{uuid}不是一个数字。此外,输入字符串本质上是动态的,因此这种匹配不是通用的解决方案此查询有效!感谢您提供的解决方案。此查询仅在{uuid}为个位数时匹配。例如:它与演示示例中以下查询的id=3不匹配:从测试中选择*,其中“/services/system/1001/group/2001/port”~concat(“^”,replace(url,“{uuid}”,“[^/]”,“$”)不应该将“{uuid}”替换为“[\w+]”吗?我的{uuid}是字母数字的。向下投票,因为这不是一个通用的解决方案。实际上,我错过了其中的
+
。它应该是
[^/]+
。如果要限制为字母数字,则可以使用
[\w]+
。但是如果它是一个statdard
uuid
,那么它应该是'[\w-]+`。我已经更新了答案