1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
create or replace package regexp# is type t_char_tab is table of varchar2(32767); function regexp_split( i_string varchar2 ,i_regexp varchar2 ,i_mode varchar2 := '' ) return t_char_tab; end regexp#; / create or replace package body regexp# is function regexp_split( i_string varchar2 ,i_regexp varchar2 ,i_mode varchar2 := '' ) return t_char_tab is l_arr t_char_tab := t_char_tab(); l_occurrence pls_integer; l_pos_start pls_integer := 1; l_pos_end pls_integer := 1; begin l_occurrence := nvl(regexp_count(i_string, i_regexp, 1, i_mode),0)+1; l_arr.extend(l_occurrence); for i in 1..l_occurrence loop if i = l_occurrence then l_pos_end := length(i_string)+1; else l_pos_end := regexp_instr(i_string, i_regexp, 1, i, 0, i_mode); end if; l_arr(i) := substr(i_string, l_pos_start, l_pos_end - l_pos_start); l_pos_start := regexp_instr(i_string, i_regexp, 1, i, 1, i_mode); end loop; return l_arr; end regexp_split; end regexp#; / -- example usage declare l_arr regexp#.t_char_tab; begin l_arr := regexp#.regexp_split('test1 test2 test3',' +'); for i in 1..l_arr.count loop dbms_output.put_line('>' || l_arr(i) || '<'); end loop; end; / |