sql - 从 regexp_matches 结果中获取第二个匹配项
问题描述
我有一个name
看起来像这样的列:
'1234567 - 7654321 - some - more - text'
我需要得到一个字符串"7654321"
。我坚持以下几点:
SELECT regexp_matches('1234567 - 7654321 - some - more - text', '\d+', 'g');
regexp_matches
----------------
{1234567}
{7654321}
(2 rows)
我怎么做我想要的?也许有比regexp_matches
- 很乐意考虑的更好的选择。谢谢!
解决方案
regexp_matches()
返回一个表,因此您可以在 from 子句中将其与with ordinality
选项一起使用:
SELECT t.value
from regexp_matches('1234567 - 7654321 - some - more - text', '\d+', 'g') with ordinality as t(value,idx)
where t.idx = 2;
请注意,value
它仍然是一个数组,要获取您可以使用的实际数组元素:
SELECT t.value[1]
from regexp_matches('1234567 - 7654321 - some - more - text', '\d+', 'g') with ordinality as t(value,idx)
where t.idx = 2;
推荐阅读
- rust - 使用 serde / serde-xml-rs 处理格式错误的 XML
- java - 服务器在 Socket Java 编程中没有响应
- yii2 - 通过作曲家让 intl-tel-input 作为 yii2 自动加载器工作
- mongodb - MongoDB - 仅获取与 ID 数组匹配的文档
- ethereum - Solidity 智能合约解析错误帮助 - 包含完整合约
- python - 如何从第一列中的名称列出/标记的最后一列中去除浮动?
- javascript - Safari 不会在 PUT 请求的正文中上传图像文件
- angular - Angular12/rxjs:更新模板中的可观察数据并显示模板中的更改
- swiftui - iOS15 - SwiftUI WheelPicker 可滚动外框和剪裁区域破坏其他界面
- spring - Spring Security:hasRole 不起作用 & AuthenticaionPrincipal 为空