sql - 在 pgSQL select 中使用正则表达式捕获

标签 sql regex postgresql

我从未在 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/

相关文章:

SQL Server 其中 start_date 和 End_date 之间的日期

javascript - 嵌套元素中的文本替换

regex - 为什么 "\b"在查找的正则表达式模式中不起作用?

sql - 有效地根据最小日期匹配两个表

sql - Postgresql:Autovacuum 分区表

postgresql - Postgres : Get numbers of affected rows in statement trigger

php - Like 语句匹配字符串的一部分

SQL 查询返回相对顺序的 int 列

Java 正则表达式验证字符串列表的开头

MYSQL选择查询包括所有如果没有指定