与此问题类似Find string match pattern。但是这次我想在PostgreSQL中做到这一点。
我试过了:
Select * from my_table where my_param SIMILAR TO "Delivered to (.*) at (.*)"
但它不起作用。
答案 0 :(得分:2)
您可以使用regexp_matches
提取部分文字:
WITH cte AS
(
SELECT col, regexp_matches(col, 'Delivered to (.*) at (.*)') AS r
FROM tab
-- WHERE col LIKE 'Delivered to % at %'
)
SELECT col, r[1] part1, r[2] part2
FROM cte;
的 SqlFiddleDemo
强>
输出:
╔═══════════════════════════════════╦═══════════╦═════════╗
║ col ║ part1 ║ part2 ║
╠═══════════════════════════════════╬═══════════╬═════════╣
║ Delivered to Mr.Smith at Seattle ║ Mr.Smith ║ Seattle ║
╚═══════════════════════════════════╩═══════════╩═════════╝
如果您只想查找行而不提取使用:
SELECT *
FROM tab
WHERE col LIKE 'Delivered to % at %'