3

I am trying to split a string and add each word to a separate rows.

with data as (
  select 100 as id, 'python java' as src from dual
)
 select id, level as lvl,
        regexp_substr( src || '" "' , '([[:space:]0-9/:])', 1, level, null, 1 ) as token
   from data
connect by level <= regexp_count( src || '" "' , '([[:space:]0-9/:])' )
       and prior id = id
       and prior sys_guid() is not null
;

I am expecting python and java in a separate rows.

Barbaros Özhan
  • 59,113
  • 10
  • 31
  • 55
Ana
  • 325
  • 2
  • 11

2 Answers2

1

Your regex seems a bit confused. I think you want something more like:

regexp_substr( src , '(.*?)([[:space:]0-9/:]|$)', 1, level, null, 1 )

db<>fiddle

Alex Poole
  • 183,384
  • 11
  • 179
  • 318
1

You can use combination of regexp_substr() and regexp_count() with connect by level<... expression :

with t as
(
  select 100 as id, 'python java' as src from dual
)
 select id, level as lvl, regexp_substr(src, '[^[:space:]]+',1,level) as token
   from t 
connect by level<= regexp_count(src,'[[:space:]]')+1
    and prior id = id
    and prior sys_guid() is not null;

Demo

Barbaros Özhan
  • 59,113
  • 10
  • 31
  • 55