WITH t AS ( SELECT '{이름}님 {1}번째 결과 {결과}입니다' m , '홍길동,2,성공' p FROM dual ) SELECT m FROM t MODEL DIMENSION BY (1 rn) MEASURES (CAST(m AS VARCHAR2(99)) m, p) RULES ITERATE (9) UNTIL INSTR(m[1], '{') = 0 ( m[1] = REGEXP_REPLACE(m[1], '[{][^}]+[}]' , REGEXP_SUBSTR(p[1], '[^,]+', 1, ITERATION_NUMBER + 1) , 1, 1) ) ;
With t(m,p) as ( select '{이름}님 {1}번째 결과 {결과}입니다' , '홍길동,2,성공' from dual UNION ALL select '{아무개}님 {?}결과 ' , '홍길동,성공' from dual ) SELECT GB , MAX(M) M , MAX(P) P , LISTAGG(REPLACE('{'||STR,V1,V2 ) ) WITHIN GROUP (ORDER BY LV ) STR FROM ( SELECT A.* , LV , REGEXP_SUBSTR(A.M , '[{](.*?)[}]' , 1 ,B.LV ) V1 , REGEXP_SUBSTR(A.P , '[^,]+' , 1 ,B.LV ) V2 , REGEXP_SUBSTR(A.M , '[^{]+' , 1 ,B.LV ) STR FROM (SELECT ROWNUM GB , T.* FROM T ) A , (SELECT LEVEL LV FROM DUAL CONNECT BY LEVEL <= (SELECT MAX(REGEXP_COUNT(m,'{')) FROM T ) ) B WHERE REGEXP_COUNT(A.M,'{') >= LV ) Z GROUP BY GB
-- 중괄호가 짝이 맞지 않는 경우도 있다고 가정 -- WITH t(lv, m, p) AS ( SELECT 1 lv , '{이름}님 {1}번{째 {결과 {결과}입}니{다' m , '홍길동,2,성공' p FROM dual UNION ALL SELECT lv + 1 lv , REGEXP_REPLACE(m, '[{][^{}]+[}]' , REGEXP_SUBSTR(p, '[^,]+', 1, lv) , 1, 1) m , p FROM t WHERE lv <= REGEXP_COUNT(p, '[^,]+') ) SELECT m FROM t WHERE lv = REGEXP_COUNT(p, '[^,]+') + 1 ;
WITH t AS ( SELECT '{ㅁㅁ{ㅂㅂ}ㅅㅅ}{ㅇㅇ}{ㅇㅇ}' m , 'A,B,C' p FROM dual ) SELECT x FROM t MODEL DIMENSION BY (1 rn) MEASURES (m, p, CAST(m AS VARCHAR2(99)) x) RULES ITERATE (9) UNTIL REGEXP_SUBSTR(p[1], '[^,]+', 1, ITERATION_NUMBER + 1) IS NULL ( x[1] = REGEXP_REPLACE(x[1] , REPLACE(REPLACE( REGEXP_SUBSTR(m[1], '[{][^}{]+[}]', 1, ITERATION_NUMBER + 1) , '{', '\{'), '}', '\}') , REGEXP_SUBSTR(p[1], '[^,]+', 1, ITERATION_NUMBER + 1) , 1, 1) ) ;
-- 단건 with t(m,p) as ( select '{이름}님 {1}번째 결과 {결과}입니다.' , '홍길동,3,성공' from dual ), t1(m,p,s) as ( select m , p , 1 from t union all select regexp_replace(m,'{([^{}]*[^{}])}',regexp_substr(p,'([^,]*)(,|$)',1,1,'i',1),s,1) , case when instr(p,',') > 0 then substr(p,instr(p,',')+1) end , regexp_instr(m,'{([^{}]*[^{}])}',s) from t1 where p is not null ) select m from t1 where p is null; -- 다건 with t(m,p) as ( select '{이름}님 {1}번째 결과 {결과}입니다.','홍길동,3,성공' from dual union all select '{이름}님 {1}번째 결과 {결과}입니다.({사유})','홍길순,2,실패,요건미달' from dual union all select '{이름}님 {결과}입니다.','홍길동,완료' from dual ) select listagg(regexp_substr(a.m,'([^ˇ]*)(ˇ|$)',1,b.no,'i',1)||regexp_substr(a.p,'[^,]+',1,b.no)) within group (order by b.no) as m from (select regexp_replace(m,'{([^{}]*[^{}])}','ˇ') m , p from t) a inner join (select level no from dual connect by level <= 10) b on b.no <= regexp_count(a.m,'ˇ') + 1 group by a.m;
WITH t AS ( SELECT '{ㅁㅁ{ㅂㅂ}ㅅㅅ}{ㅇㅇ}{ㅇㅇ}' m, 'A,B,C' p FROM dual UNION ALL SELECT '{이름}님 {1}번째 결과 {결과}입니다.', '홍길동,3,성공' FROM dual UNION ALL SELECT '{이름}님 {1}번째 결과 {결과}입니다.({사유})', '홍길순,2,실패,요건미달' FROM dual UNION ALL SELECT '{이름}님 {결과}입니다.', '홍길동,완료' FROM dual ) SELECT m FROM t MODEL DIMENSION BY (ROWNUM rn) MEASURES (REGEXP_REPLACE(m, '{[^}{]+}', '♪') m, p) RULES ITERATE (9) ( m[ANY] = REGEXP_REPLACE(m[CV()], '♪' , REGEXP_SUBSTR(p[CV()], '[^,]+', 1, ITERATION_NUMBER + 1) , 1, 1) ) ;
WITH t AS ( SELECT '{ㅁㅁ{ㅂㅂ}ㅅㅅ}{ㅇㅇ}{ㅇㅇ}' m, 'A,B,C' p FROM dual UNION ALL SELECT '{이름}님 {1}번째 결과 {결과}입니다.', '홍길동,3,성공' FROM dual UNION ALL SELECT '{이름}님 {1}번째 결과 {결과}입니다.({사유})', '홍길순,2,실패,요건미달' FROM dual UNION ALL SELECT '{이름}님 {결과}입니다.', '홍길동,완료' FROM dual ) , t1 (m, p) AS ( SELECT REGEXP_REPLACE(m, '{[^}{]+}', '♪') m , p FROM t UNION ALL SELECT REGEXP_REPLACE(m, '♪', REGEXP_SUBSTR(p, '[^,]+'), 1, 1) , REGEXP_REPLACE(p, '^,?[^,]+') p FROM t1 WHERE INSTR(m, '♪') > 0 ) SELECT * FROM t1 WHERE INSTR(m, '♪') = 0 ;