如何在oracle中使用特殊字符作为分隔符来grep字符串?

时间:2017-02-05 10:30:59

标签: sql oracle

我想从以下模式

中获取在第一个特殊字符之前结束的ip地址
SQL> select distinct cell_name from v$cell_state;

CELL_NAME
--------------------------------------------------------------------------------
10.160.0.39;10.160.0.40
10.160.0.41;10.160.0.42
10.160.0.43;10.160.0.44
10.160.0.45;10.160.0.46
10.160.0.47;10.160.0.48
10.160.0.49;10.160.0.50
10.160.0.51;10.160.0.52

expected output:
10.160.0.39
10.160.0.41
10.160.0.43
10.160.0.45
10.160.0.47
10.160.0.49
10.160.0.51

SQL> select distinct cell_name from v$cell_state;

CELL_NAME
--------------------------------------------------------------------------------
10.160.0.39,10.160.0.40
10.160.0.41,10.160.0.42
10.160.0.43,10.160.0.44
10.160.0.45,10.160.0.46
10.160.0.47,10.160.0.48
10.160.0.49,10.160.0.50
10.160.0.51,10.160.0.52

expected output:
10.160.0.39
10.160.0.41
10.160.0.43
10.160.0.45
10.160.0.47
10.160.0.49
10.160.0.51

SQL> select distinct cell_name from v$cell_state;

CELL_NAME
--------------------------------------------------------------------------------
190.160.14.3
190.160.14.4
190.160.14.5
190.160.14.8
190.160.14.9

expected output:
190.160.14.3
190.160.14.4
190.160.14.5
190.160.14.8
190.160.14.9

我想编写一个查询来实现它在以上所有3场景中获取ip#s即使没有特殊字符,它也应该获取现有的ip输出

1 个答案:

答案 0 :(得分:1)

[^;, ];,以外的任何章程 *任意数量的事件

select  distinct 
        regexp_substr(cell_name,'[^;, ]*')  

from    v$cell_state
;

演示

select  regexp_substr('10.160.0.39;10.160.0.40' ,'[^;, ]*')
       ,regexp_substr('10.160.0.39,10.160.0.40' ,'[^;, ]*')
       ,regexp_substr('10.160.0.39'             ,'[^;, ]*')

from    dual
;