[英]Split a column into multiple rows based on multiple delimiters in Postgres
我在 postgres 11 中有下表。
col1
a;b;c
d/e/f
g and h
i with k
l + m
我想根据某些分隔符 (/,+,;,and,with]) 拆分此表。 所需的 output 是:
col1 col2
a;b;c a
a;b;c b
a;b;c c
d/e/f d
d/e/f e
d/e/f f
g and h g
g and h h
i with k i
i with k k
l + m l
l + m m
我正在尝试以下查询。
select distinct * from table t,
UNNEST(REGEXP_SPLIT_TO_ARRAY(t.col1, '\s+[/|+|;|and|with]\s+')) s(col2)
您可以使用
[/+;]|\s+(?:and|with)\s+
\s*(?:[/+;]|and|with)\s*
请参阅正则表达式演示 #1和正则表达式演示 #2 。
细节
[/+;]
- 一个+
, /
或;
字符 - |
- 或者\s+(?:and|with)\s+
- 一个and
或with
一个或多个空格的单词\s*(?:[/+;]|and|with)\s*
匹配/
, +
, ;
or and
or with
0 个或多个空格字符括起来的字符串。 注意(?:...)
是一个非捕获组,当分组构造匹配的值以后不会被检索/使用时,这是一个自然选择。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.