2017-04-26 83 views
0

我想在分割方括号中的所有值的列执行正则表达式后产生多行。目前,我只能返回一个值。Oracle正则表达式连接通过

我执行正则表达式的字段有此值:

[1265] * [1263]

我试图让1265和1263在我的结果设置为不同的行。

SELECT REGEXP_SUBSTR(column,'\[(.*?)\]',1,LEVEL) AS "col1" 
FROM table 
CONNECT BY REGEXP_SUBSTR(column,'\[(.*?)\]',1,LEVEL) IS NOT NULL; 

相反,我只是在结果集中得到这个。

[1263]

+0

这些值是否始终为整数? – Aleksej

+0

当我运行它时,你的代码将该字符串分成两行。 – APC

+0

它确实返回两​​行,但其中一行为空,另一行填充。 – user1060187

回答

0
with test (rn, col) as 
( 
    select 'a', '[123]*[abc] []' from dual union all 
    select 'b', '[45][def] ' from dual union all 
    select 'c', '[678],.*' from dual 
), 
coll (rn, col) as 
(
    select rn,regexp_replace(col, '(\[.*?\])|.', '\1') from test 
), 
cte (rn, cnt, col, i) as 
(
    select rn, 1, col, regexp_substr(col, '(\[(.*?)\])', 1, 1, null, 2) 
    from coll 
    union all 
    select rn, cnt+1, col, regexp_substr(col, '(\[(.*?)\])', 1, cnt+1, null, 2) 
    from cte 
    where cnt+1 <= regexp_count(col, '\[.*?\]') 
) 
select * from cte 
order by 1,2; 
0

此正则表达式通过寻找闭括号计数元件,并返回该括号内的数字,从而允许空值。因为你想要的数据元素被方括号包围,所以我们可以把重点放在这些分隔符上。

SQL> with test(rownbr, col) as (
     select 1, '[1265]**[1263]' from dual union 
     select 2, '[123]'   from dual union 
     select 3, '[111][222]*[333]' from dual union 
     select 4, '[411]*[][433]' from dual 
    ) 
    select distinct rownbr, level as element, 
      regexp_substr(col, '\[([0-9]*)\]', 1, level, null, 1) value 
    from test 
    connect by level <= regexp_count(col, ']') 
    order by rownbr, element; 

    ROWNBR ELEMENT VALUE 
---------- ---------- ----- 
     1   1 1265 
     1   2 1263 
     2   1 123 
     3   1 111 
     3   2 222 
     3   3 333 
     4   1 411 
     4   2 
     4   3 433 

9 rows selected. 

SQL>