我有一个包含两列的表:ID和汽车。 示例数据如下所示:
ID | Cars
-----------------
1 | opel, honda and land rover
2 | ford and porshe, damaged
3 | volkswagen
4 | opel, seat, damaged
我想把它分成:
ID | Cars
-----------------
1 | opel
1 | honda
1 | land rover
2 | ford
2 | porshe, damaged
3 | volkswagen
4 | opel
4 | seat, damaged
因此分隔符为and
或,
,但不是, damaged
如何使用正则表达式在postgresql中拆分它?
修改
如何使其适用于
等记录5 | land rover and opel, and ford
6 | ford; mazda and toyota
答案 0 :(得分:4)
您可以使用regexp_split_to_table
带有负面预测功能进行损坏;
SELECT "ID", regexp_split_to_table("Cars", '((, (?!damaged))| and )') "Cars"
FROM mytable;
ID | Cars
----+-----------------
1 | opel
1 | honda
1 | land rover
2 | ford
2 | porshe, damaged
3 | volkswagen
4 | opel
4 | seat, damaged
(8 rows)
编辑:对于你的新例子,正则表达式必须稍微调整一下;
SELECT "ID", regexp_split_to_table("Cars", '(([,;] (?!damaged))|[,;]? and )') "Cars"
FROM mytable;