gpt4 book ai didi

regex - 在 PostgreSQL 中使用正则表达式提取字符串的一部分

转载 作者:行者123 更新时间:2023-11-29 11:33:59 36 4
gpt4 key购买 nike

我想提取 first_namesecond_name 字符串,但使用此正则表达式只能获取 first_name 字符串。

SELECT
SUBSTRING (
'a:2:{i:0;s:14:"first_name";i:1;s:15:"second_name";}',
'\"[^\"]*\" '
) as obs_regex

如何修改我的正则表达式以获得 first_name second_name 作为结果?

谢谢你

最佳答案

您可以使用 g 标志匹配多次出现,并且您需要使用捕获组来获取不带引号的值:

SELECT
REGEXP_MATCHES (
'a:2:{i:0;s:14:"first_name";i:1;s:15:"second_name";}',
'"([^"]*)"', 'g'
) as obs_regex

结果:

enter image description here

要获得串联的匹配字符串,您需要将 regexp_matches 结果“转换”为数组并使用 array_to_string:

SELECT ARRAY_TO_STRING (
ARRAY (
SELECT REGEXP_MATCHES (
'a:2:{i:0;s:14:"first_name";i:1;s:15:"second_name";}',
'"([^"]*)"', 'g'
)
), ' ') as obs_regex

结果:

enter image description here

关于regex - 在 PostgreSQL 中使用正则表达式提取字符串的一部分,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57669480/

36 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com