[英]Split a column into multiple rows based on multiple delimiters in Postgres
I have following table in postgres 11.我在 postgres 11 中有下表。
col1
a;b;c
d/e/f
g and h
i with k
l + m
I would like to split this table based on certain delimiters (/,+,;,and,with]).我想根据某些分隔符 (/,+,;,and,with]) 拆分此表。 The desired output is:
所需的 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
I am trying out below query.我正在尝试以下查询。
select distinct * from table t,
UNNEST(REGEXP_SPLIT_TO_ARRAY(t.col1, '\s+[/|+|;|and|with]\s+')) s(col2)
You may use您可以使用
[/+;]|\s+(?:and|with)\s+
\s*(?:[/+;]|and|with)\s*
See the regex demo #1 and regex demo #2 .请参阅正则表达式演示 #1和正则表达式演示 #2 。
Details细节
[/+;]
- a +
, /
or ;
[/+;]
- 一个+
, /
或;
char - |
|
- or \s+(?:and|with)\s+
- an and
or with
words enclosed with one or more whitespaces \s+(?:and|with)\s+
- 一个and
或with
一个或多个空格的单词\s*(?:[/+;]|and|with)\s*
matches a /
, +
, ;
\s*(?:[/+;]|and|with)\s*
匹配/
, +
, ;
or and
or with
strings enclosed with 0 or more whitespace chars. and
or with
0 个或多个空格字符括起来的字符串。 Note (?:...)
is a non-capturing group that is a natural choice when the value matched by a grouping construct is not going to be retrieved/used later.注意
(?:...)
是一个非捕获组,当分组构造匹配的值以后不会被检索/使用时,这是一个自然选择。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.