在pgSQL select中使用正则表达式捕获
我从未在SQL中使用过正则表达式。如果我在javascript中捕获了类似的内容,那么如何像使用javascript匹配方法中的第二个元素一样在pgSQL中引用该捕获 我怎样才能把它放到SELECT语句中,这样我就可以说,从thomas-var-3中检索我想要的值 这有点像在pgSQL select中使用正则表达式捕获,sql,regex,postgresql,Sql,Regex,Postgresql,我从未在SQL中使用过正则表达式。如果我在javascript中捕获了类似的内容,那么如何像使用javascript匹配方法中的第二个元素一样在pgSQL中引用该捕获 我怎样才能把它放到SELECT语句中,这样我就可以说,从thomas-var-3中检索我想要的值 这有点像 SELECT * FROM forms WHERE name LIKE '%bill%' AND category ='the value i want' 使用subselect和substring方法,您应该能够实现您想
SELECT * FROM forms WHERE name LIKE '%bill%' AND category ='the value i want'
使用subselect和substring方法,您应该能够实现您想要的结果,如下所示:
SELECT *
FROM firstTable
WHERE parentCat = (SELECT cat
FROM secondTable
WHERE cat in substring(column_to_capture_from from 'thomas-var-3="(.+?)' ));
从
如果存在匹配项,并且正则表达式有一个或多个捕获组,则返回与第一个捕获组匹配的文本
您可以使用not regex,但为此,请执行以下操作:
str := 'thomas-var1="SOME VAL1" thomas-var2="SOME VAL2" thomas-var-3="the value i want"'
str := replace(replace(str, '=', '=>'), '" ', '", ')
select *
from forms
where name like '%bill%' and category = hstore(str)->'thomas-var-3'
你能发布一些输入和输出的例子吗?还有-你通读了吗?@ruakh更新。谢谢。您是否直接从javascript与postgres进行交互?您使用什么技术从javascript转换到SQL语句?@JDiPierro javascript不参与其中。这是我知道如何编写模式的唯一方法,所以我在问题中用它来演示我试图用SQL做什么。啊,我明白了。我不知道你想和什么比赛。。您要填写的匹配项。。是否希望postgres在搜索时将其从其他列中拉出?如果子选择返回多行,则使用IN而不是=更安全。
SELECT *
FROM firstTable
WHERE parentCat = (SELECT cat
FROM secondTable
WHERE cat in substring(column_to_capture_from from 'thomas-var-3="(.+?)' ));
str := 'thomas-var1="SOME VAL1" thomas-var2="SOME VAL2" thomas-var-3="the value i want"'
str := replace(replace(str, '=', '=>'), '" ', '", ')
select *
from forms
where name like '%bill%' and category = hstore(str)->'thomas-var-3'