我从未在 SQL 中使用过正则表达式。如果我在 javascript 中捕获类似的内容,如何像使用 javascript match()
方法中的第二个元素一样引用 pgSQL 中的捕获?
var str = 'thomas-var1="SOME VAL1" thomas=var2="SOME VAL2" thomas-var-3="the value i want" thomas-var-4="SOME_VAL4"';
var re = /thomas-var-3="(.+?)"/i;
var new_str = str.match(re);
console.log(new_str[1]);
如何将其放入 SELECT
语句中,以便我可以说类似的话,从 thomas-var 检索值
?“我想要的值”
-3
SELECT * FROM forms WHERE name LIKE '%bill%' AND category = MY REGEX CAPTURE
渲染类似“
SELECT * FROM forms WHERE name LIKE '%bill%' AND category ='the value i want'
最佳答案
您可以不使用正则表达式,而是使用 hstore extension为此,例如:
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'
关于sql - 在 pgSQL select 中使用正则表达式捕获,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18468254/