sql - 在 where 子句中的 Postgres 正则表达式
问题描述
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
我尝试了以下查询来匹配第 3 行,但它不起作用:
select * from test where regexp_replace(url, '{uuid}', '*', 'g') ~ '/services/system/1/group/1/port'
有什么解决办法吗?
解决方案
考虑到您在问题和评论中给出的场景,您可以使用以下查询:
select * from test
where '/services/system/1/group' ~ concat('^',replace(url,'{uuid}','[^/]+'),'$')
在这里它将检查除/
代替之外的任何字符{uuid}
编辑 如果你只想要字母数字,那么你应该使用试试这个:
select * from test
where '/services/system/1/group' ~ concat('^',replace(url,'{uuid}','[\w]+'),'$')
推荐阅读
- javascript - 如何使用 Web Server for Chrome 读取和处理本地文件
- javascript - 用于在立方投资组合插件中加载更多按钮的回调函数
- azure-resource-manager - ARM 模板 Key Vault - “accessPolicies”的值无效
- r - 将框和线组合成单个 R 图表图例
- nginx - 如何将 Vue 项目部署到生产环境?
- android - 如何在 Android 片段工具栏上创建搜索视图?
- php - 如何在同一页面上显示表单错误并保留用户输入的数据?
- github - 使用 GitHub API 在特定月份获取 GitHub 存储库上的所有问题
- mysql - 选择不包含来自其他表的引用的记录
- google-cloud-platform - Google Bigquery 是否适合从 IoT 设备插入数据?