Вы можете использовать
with t(str) as
(
select 'the ID''s are [tag1] , [tag2] , [tag3]' from dual
), t2(str2) as
(
select regexp_substr(str,'[^\[]+', 1, 1) from t
)
select concat( regexp_substr(str,'[^\[]+', 1),
listagg(replace(regexp_substr(str,'[^\[]*.[^\]]', 1, level),']',''),',')
within group (order by 1) )
as "Derived String"
from t
cross join t2
connect by level <= regexp_count(str,'\[');
Derived String
---------------------------
the ID's are tag1,tag2,tag3
Демо
Редактировать 1 : Если вы хотите динамически извлекать только теги как
tag1 tag2 tag3 .... tag n
затем используйте ниже
with t(str) as
(
select 'the ID''s are [tag1] , [tag2] , [tag3]' from dual
)
select listagg(replace(regexp_substr(str,'[^\[]*.[^\]]', 1, level),']',''),' ')
within group (order by 1)
as "Derived String"
from t
connect by level <= regexp_count(str,'\[')
Редактировать 2 (из-за последнего комментария):
Попробуйте использовать ниже
with t(a,b) as
(
select 'the ID''s are [tag1] , [tag2] , [tag3]' as a,
'the ID''s are [tag4] , [tag5] , [tag6], [tag7]' as b
from dual
)
select listagg(replace(regexp_substr(a,'[^\[]*.[^\]]', 1, level),']',''),' ')
within group (order by 1)
as "Derived String 1",
listagg(replace(regexp_substr(b,'[^\[]*.[^\]]', 1, level),']',''),' ')
within group (order by 1)
as "Derived String 2"
from t
connect by level <= greatest(regexp_count(a,'\['),regexp_count(b,'\['));
Derived String 1 Derived String 2
--------------------------- ---------------------------
tag1 tag2 tag3 tag4 tag5 tag6 tag7