Split string by space and character as delimiter in Oracle with regexp_substr

前端 未结 5 2027
太阳男子
太阳男子 2020-12-04 00:32

I\'m trying to split a string with regexp_subtr, but i can\'t make it work.

So, first, i have this query

select regexp_substr(\'Helloworld - test!\'          


        
5条回答
  •  被撕碎了的回忆
    2020-12-04 00:44

    Slight improvement on MT0's answer. Dynamic count using regexp_count and proves it handles nulls where the format of [^delimiter]+ as a pattern does NOT handle NULL list elements. More info on that here: Split comma seperated values to columns

    SQL> with tbl(str) as (
      2    select ' - Hello world - test-test! -  - test - ' from dual
      3  )
      4  SELECT LEVEL AS Occurrence,
      5         REGEXP_SUBSTR( str ,'(.*?)([[:space:]]-[[:space:]]|$)', 1, LEVEL, NULL, 1 ) AS split_value
      6  FROM   tbl
      7  CONNECT BY LEVEL <= regexp_count(str, '[[:space:]]-[[:space:]]')+1;
    
    OCCURRENCE SPLIT_VALUE
    ---------- ----------------------------------------
             1
             2 Hello world
             3 test-test!
             4
             5 test
             6
    
    6 rows selected.
    
    SQL>
    

提交回复
热议问题