There is a line '/home/pc/test' ,
and words in tags '[/alseko][/logs][/archive]'
How to get the expected result =

  [/home/pc/test/alseko][/home/pc/test/logs][/home/pc/test/archive] 

My code trying to find:

 select '[' || '/home/pc/test' || ltrim(substr('[/alseko][/logs][/archive]', instr('[/alseko][/logs][/archive]', '['), instr('[/alseko][/logs][/archive]', ']')),'[') from dual 
  • one
    You will first have to disassemble the line into its components, having received several lines, then attach the path and assemble it back into one line (for example, listagg). You can take this analysis as a basis ru.stackoverflow.com/a/583340/194569 - Mike

2 answers 2

You need to parse the tags into a table, and then reassemble them into a string, for example:

 with data as ( select '[/alseko][/logs][/archive]' tags, '\[(\/\w+)' pattern, '/home/pc/test' prefix from dual ), tags as ( select '['||prefix||regexp_substr (tags, pattern, 1, level, null, 1)||']' tag, level sort from data connect by level <= regexp_count (tags, pattern) ) select listagg (tag) within group (order by sort) from tags ; 

Conclusion:

[/ home / pc / test / alseko] [/ home / pc / test / logs] [/ home / pc / test / archive]

    It turned out to do through the cycle, first determined the number of occurrences

     Declare vn_count_paths Integer; vc_paths varchar(1024); vc_FtpDir varchar(1024); begin vn_count_paths:= length('[/alseko][/logs][/archive]')-LENGTH(REPLACE('[/alseko][/logs][/archive]','[')); dbms_output.put_line('Size: ' || vn_count_paths); for i in 1 .. vn_count_paths loop vc_paths:= vc_paths||'['||'/home/pc/test'||ltrim(substr('[/alseko][/logs][/archive]',instr('[/alseko][/logs][/archive]','[',1,i)+1, instr('[/alseko][/logs][/archive]',']',1,i)- instr('[/alseko][/logs][/archive]','[',1,i)-1),'[')||']'; end loop; vc_FtpDir := '['||'/home/pc/test'||']'||vc_paths; dbms_output.put_line('vc_FtpDir: ' || vc_FtpDir); end;