PostgreSql中表中的正则表达式搜索
我试图在PostgreSql中对表进行正则表达式搜索。但我无法找到正确的语法来使用单个语句。 我不知道我是否创建了索引列,我是否能够基于正则表达式进行搜索。有人能帮我吗 查询是这样的PostgreSql中表中的正则表达式搜索,postgresql,Postgresql,我试图在PostgreSql中对表进行正则表达式搜索。但我无法找到正确的语法来使用单个语句。 我不知道我是否创建了索引列,我是否能够基于正则表达式进行搜索。有人能帮我吗 查询是这样的 SELECT * FROM flight_info where to_tsvector('english',event_id || ' ' || type || ' ' || category || ' ' || number || ' '|| make || ' '|| model ) @@ to_tsqu
SELECT *
FROM flight_info
where to_tsvector('english',event_id || ' ' || type || ' ' || category || ' ' || number || ' '|| make || ' '|| model ) @@ to_tsquery('Incident');
在这里,事件完全是基于文本匹配的,但我需要使搜索基于正则表达式,就像我们使用like所做的那样
或者对所有列使用like子句是唯一的方法
或者有没有一种方法可以给出like子句,它适用于查询中的许多列您将无法混合使用正则表达式和全文搜索。无法在
to_tsquery()中使用正则表达式
您可以改用regexp\u matches()
:
SELECT regexp_matches('foobarbequebaz', '(bar)(beque)');
regexp_matches
----------------
{bar,beque}
(1 row)
SELECT regexp_matches('foobarbequebazilbarfbonk', '(b[^b]+)(b[^b]+)', 'g');
regexp_matches
----------------
{bar,beque}
{bazil,barf}
(2 rows)
更多信息请访问
更新:
要在wheWHERE
子句中使用正则表达式,请使用~
运算符:
-- Sample schema
CREATE TABLE sample_table (
id serial,
col1 text,
col2 text,
col3 text
);
INSERT INTO sample_table (col1, col2, col3) VALUES ('this', 'is', 'foobarbequebazilbarfbonk');
INSERT INTO sample_table (col1, col2, col3) VALUES ('apple foobarbequebazequeb', 'rocky', 'sunny');
INSERT INTO sample_table (col1, col2, col3) VALUES ('not', 'a', 'match');
-- Query
SELECT *
FROM sample_table
WHERE (col1 || col2 || col3) ~ '(bar)(beque)';
->
您可以使用~*
使其不区分大小写。
更多信息:您能建议我如何将此应用于专栏吗。我想将其应用于多列shi@harshakumarReddy我更新了我的答案,以显示如何在oncecan使用多列,我们使用上面where子句中所示的regexp_匹配。我无法让它工作。我需要根据正则表达式选择列search@harshakumarReddy我用一个操作符~
的例子更新了我的答案,你可以用它来匹配WHERE
子句中的正则表达式
id | col1 | col2 | col3
----+---------------------------+-------+--------------------------
1 | this | is | foobarbequebazilbarfbonk
2 | apple foobarbequebazequeb | rocky | sunny
(2 rows)