http://www.2cto.com/database/201209/154045.html


Oracle中REGEXP_SUBSTR函数
Oracle中REGEXP_SUBSTR函数的使用说明:
题目如下:
在oracle中,使用一条语句实现将'17,20,23'拆分成'17','20','23'的集合。
REGEXP_SUBSTR函数格式如下:
function REGEXP_SUBSTR(String, pattern, position, occurrence, modifier)
__srcstr     :需要进行正则处理的字符串
__pattern    :进行匹配的正则表达式
__position   :起始位置,从第几个字符开始正则表达式匹配(默认为1)
__occurrence :标识第几个匹配组,默认为1
__modifier   :模式('i'不区分大小写进行检索;'c'区分大小写进行检索。默认为'c'。)
1、查询使用正则分割后的第一个值,也就是17
[sql] 
SELECT REGEXP_SUBSTR('17,20,23','[^,]+',1,1,'i') AS STR FROM DUAL;  
结果:  www.2cto.com  
STR
-----
17
2、查询使用正则分割后的最后一个值,也就是23
[sql] 
SELECT REGEXP_SUBSTR('17,20,23','[^,]+',1,3,'i') AS STR FROM DUAL;  
结果:
STR
----
23
3、获取一个多个数值的列,从而能够让结果以多行的形式展示出来
[sql] 
SELECT LEVEL FROM DUAL CONNECT BY LEVEL <=7;  
结果:  www.2cto.com  
LEVEL
----
1
2
3
4
5
6
7
4、将上面REGEXP_SUBSTR的occurrence关联
[sql] 
SELECT NVL(REGEXP_SUBSTR('17,20,23', '[^,]+', 1, LEVEL, 'i'), 'NULLL') AS STR  
FROM DUAL  
CONNECT BY LEVEL <= 7;  
STR  www.2cto.com  
----
17
20
23
NULL
NULL
NULL
NULL
5、优化上面的SQL语句,让生成的行的数量符合实际情况
[sql] 
SELECT REGEXP_SUBSTR('17,20,23', '[^,]+', 1, LEVEL, 'i') AS STR  
FROM DUAL  
CONNECT BY LEVEL <=  
LENGTH('17,20,23') - LENGTH(REGEXP_REPLACE('17,20,23', ',', ''))+1;  
STR
----
17
20

23

=====================

select regexp_substr(p_RecovTypeRule, '[^,]+', 1, level) as recoveryType from dual
    connect by regexp_substr(p_RecovTypeRule, '[^,]+', 1, level) is not null;

=================

http://www.weste.net/2010/9-18/72581.html

Oracle使用正则表达式离不开这4个函数:

1。regexp_like

2。regexp_substr

3。regexp_instr

4。regexp_replace

看函数名称大概就能猜到有什么用了。

regexp_like 只能用于条件表达式,和 like 类似,但是使用的正则表达式进行匹配,语法很简单:

regexp_substr 函数,和 substr 类似,用于拾取合符正则表达式描述的字符子串,语法如下:

regexp_instr 函数,和 instr 类似,用于标定符合正则表达式的字符子串的开始位置,语法如下:

regexp_replace 函数,和 replace 类似,用于替换符合正则表达式的字符串,语法如下:

这里解析一下几个参数的含义:

1。source_char,输入的字符串,可以是列名或者字符串常量、变量。

2。pattern,正则表达式。

3。match_parameter,匹配选项。

取值范围: i:大小写不敏感; c:大小写敏感;n:点号 . 不匹配换行符号;m:多行模式;x:扩展模式,忽略正则表达式中的空白字符。

4。position,标识从第几个字符开始正则表达式匹配。

5。occurrence,标识第几个匹配组。

6。replace_string,替换的字符串。

说了一堆文绉绉的,现在开始实例演练了,在此之前先建好一个表。

01 create table tmp as

02 with data as (

03   select 'like' as id ,'a9999' as str from dual union all

04   select 'like'       ,'a9c'          from dual union all

05   select 'like'       ,'A7007'        from dual union all

06   select 'like'       ,'123a34cc'     from dual union all

07   select 'substr'     ,'123,234,345'  from dual union all

08   select 'substr'     ,'12,34.56:78'  from dual union all

09   select 'substr'     ,'123456789'    from dual union all

10   select 'instr'      ,'192.168.0.1'  from dual union all

11   select 'replace'    ,'(020)12345678' from dual union all

12   select 'replace'    ,'001517729C28' from dual

13 )

14 select * from data ;

15

16 select * from tmp ;

17 ID      STR

18 ------- -------------

19 like    a9999

20 like    a9c

21 like    A7007

22 like    123a34cc

23 substr  123,234,345

24 substr  12,34.56:78

25 substr  123456789

26 instr   192.168.0.1

27 replace (020)12345678

28 replace 001517729C28

regexp_like 例子:

01 select str from tmp where id='like' and regexp_like(str,'A\d+','i'); -- 'i' 忽略大小写

02 STR

03 -------------

04 a9999

05 a9c

06 A7007

07 123a3

4cc

08

09 select str from tmp where id='like' and regexp_like(str, 'a\d+');

10 STR

11 -------------

12 a9999

13 a9c

14 123a34cc

15

16 select str from tmp where id='like' and regexp_like(str,'^a\d+');

17 STR

18 -------------

19 a9999

20 a9c

21

22 select str from tmp where id='like' and regexp_like(str,'^a\d+$');

23 STR

24 -------------

25 a9999

regexp_substr 例子:

01 col str format a15;

02 select

03   str,

04   regexp_substr(str,'[^,]+')     str,

05   regexp_substr(str,'[^,]+',1,1) str,

06   regexp_substr(str,'[^,]+',1,2) str,  -- occurrence 第几个匹配组

07   regexp_substr(str,'[^,]+',2,1) str   -- position 从第几个字符开始匹配

08 from tmp

09 where id='substr';

10 STR             STR             STR             STR             STR

11 --------------- --------------- --------------- --------------- ---------------

12 123,234,345     123             123             234             23

13 12,34.56:78     12              12              34.56:78        2

14 123456789       123456789       123456789                       23456789

15

16 select

17   str,

18   regexp_substr(str,'\d')        str,

19   regexp_substr(str,'\d+'  ,1,1) str,

20   regexp_substr(str,'\d{2}',1,2) str,

21   regexp_substr(str,'\d{3}',2,1) str

22 from tmp

23 where id='substr';

24 STR             STR             STR             STR             STR

25 --------------- --------------- --------------- --------------- ---------------

26 123,234,345     1               123             23              234

27 12,34.56:78     1               12              34

28 123456789       1               123456789       34              234

29

30

31 select regexp_substr('123456789','\d',1,level) str  --取出每位数字,有时这也是行转列的方式

32 from dual

33 connect by level<=9

34 STR

35 ---------------

36 1

37 2

38 3

39 4

40 5

41 6

42 7

43 8

44 9

regex_instr 例子:

01 col ind format 9999;

02 select

03   str,

04   regexp_instr(str,'\.'    ) ind ,

05   regexp_instr(str,'\.',1,2) ind ,

06   regexp_instr(str,'\.',5,2) ind

07 from tmp where id='instr';

08 STR               IND   IND   IND

09 --------------- ----- ----- -----

10 192.168.0.1         4     8    10

11

12 select

13   regexp_instr('192.168.0.1','\.',1,level) ind ,  -- 点号. 所在的位置

14   regexp_instr('192.168.0.1','\d',1,level) ind    -- 每个数字的位置

15 from dual

16 connect by level <=  9

17   IND   IND

18 ----- -----

19     4     1

20     8     2

21    10     3

22     0     5

23     0     6

24     0     7

25     0     9

26     0    11

27     0     0

regex_replace 例子:

01 select

02   str,

03   regexp_replace(str,'020','GZ') str,

04   regexp_replace(str,'(\d{3})(\d{3})','<\2\1>') str -- 将第一、第二捕获组交换位置,用尖括号标识出来

05 from tmp

06 where id='replace';

07 STR             STR             STR

08 --------------- --------------- ---------------

09 (020)12345678   (GZ)12345678    (020)<456123>78

10 001517729C28    001517729C28    <517001>729C28

综合应用的例子:

01 col row_line format a30;

02 with sudoku as (

03   select '020000080568179234090000010030040050040205090070080040050000060289634175010000020' as line

04   from dual

05 ),

06 tmp as (

07   select regexp_substr(line,'\d{9}',1,level) row_line,

08   level col

09   from sudoku

10   connect by level<=9

11 )

12 select regexp_replace( row_line ,'(\d)(\d)(\d)(\d)(\d)(\d)(\d)(\d)(\d)','\1 \2 \3 \4 \5 \6 \7 \8 \9') row_line

13 from tmp

14

15 ROW_LINE

16 ------------------------------

17 0 2 0 0 0 0 0 8 0

18 5 6 8 1 7 9 2 3 4

19 0 9 0 0 0 0 0 1 0

20 0 3 0 0 4 0 0 5 0

21 0 4 0 2 0 5 0 9 0

22 0 7 0 0 8 0 0 4 0

23 0 5 0 0 0 0 0 6 0

24 2 8 9 6 3 4 1 7 5

25 0 1 0 0 0 0 0 2 0

Oracle中REGEXP_SUBSTR函数相关推荐

  1. oracle逗号隔开行转列_oralce逗号分割变多行 Oracle中REGEXP_SUBSTR函数

    Oracle中REGEXP_SUBSTR函数 Oracle 中REGEXP_SUBSTR函数的使用说明: 题目如下:在 oracle 中,使用一条语句实现将'17,20,23'拆分成'17','20' ...

  2. oracle里面的nvl,oracle中nvl函数

    最近在修改项目中一个统计的bug,统计出的钱数不对,因为不是自己开发的模块,经过分析流程找到了统计的sql. sum(f_msmoney)+sum(f_fkmoney) as total, 上面这段是 ...

  3. oracle中各种函数,oracle中常用函数大全

    1.数值型常用函数 函数 返回值 样例 显示 ceil(n) 大于或等于数值n的最小整数 select ceil(10.6) from dual; 11 floor(n) 小于等于数值n的最大整数 s ...

  4. oracle中存在函数吗,Oracle中的函数

    Oracle中的函数 1.单行函数也称标量函数,对于从表中查询的每一行,该函数都返回一个值.单行函数可用与select子句中,也可用于where子句中.单行函数大致分为: >.日期函数 > ...

  5. oracle tabs作用,Oracle 中 table 函数的应用浅析

    表函数可接受查询语句或游标作为输入参数,并可输出多行数据.该函数可以平行执行,并可持续输出数据流,被称作管道式输出.应用表函数可将数据转换分阶段处理,并省去中间结果的存储和缓冲表. 1. 用游标传递数 ...

  6. oracle杀死进程时权限不足_在oracle中创建函数时权限不足

    我对oracle有一点了解.我试图创建一个如下所示的函数.在oracle中创建函数时权限不足 CREATE OR REPLACE FUNCTION "BOOK"."CON ...

  7. oracle中prad函数_等保测评2.0:Oracle身份鉴别

    一.说明 本篇文章主要说一说oracle数据库中身份鉴别控制点中测评项a的相关内容和理解. 二.测评项a a)应对登录的用户进行身份标识和鉴别,身份标识具有唯一性,身份鉴别信息具有复杂度要求并定期更换 ...

  8. oracle中的to_number,Oracle中to_number()函数的用法

    to_number()函数是oracle中常用的类型转换函数之一,是将一些处理过的按一定格式编排过的字符串变回数值型的格式. 1.to_number()函数可以将char或varchar2类型的str ...

  9. Oracle中to_char()函数的用法

    Oracle中to_char()函数的用法 日期转换: to_char(date,'格式') select to_date('2005-01-01 ','yyyy-MM-dd') from dual; ...

最新文章

  1. 做管理必须避开的六个坑
  2. mysql大表修改表名原理_MySQL修改大表工具pt-online-schema-change原理
  3. Xamarin iOS开发中的编辑、连接、运行
  4. shell / 获取当前工作目录的方法
  5. Yum mysql 日志_CentOS5上yum安装Apache+php+Mysql
  6. 硬件:台式机老式键盘知识科普
  7. [Spring MVC] - InitBinder验证
  8. centos 重启网卡_CentOS6 网络管理之网卡配置及简单路由设置
  9. 数字图像处理实验四图像频域增强
  10. openstack创建外网_OpenStack的女性谈论外展,教育和指导
  11. 学会python爬虫能发财么_python如何赚钱? python爬虫如何进阶? python就业? 如何快速入门python?...
  12. 全卷积网络 FCN 详解(很好,详看)
  13. 最近写的一个书店项目
  14. Linux 克隆虚拟机以及克隆之后引起的“Device eth0 does not seem to be present, delaying initialization”问题解决...
  15. Dreamweaver构建Blog全程实录
  16. mysql独立开发_TickyCMS: TickyCMS是由罗敏贵独自开发的一款基于PHP+Mysql架构的轻量级开源内容管理系统。...
  17. 计算机云维护是做什么的,IT运维是什么?云时代下的运维人员是怎样的?
  18. 寒假的一点笔记《123速通》
  19. 购买新款macbook pro,现在买还是等双十一?
  20. MAC 下 CocoaPods 安装与使用来管理项目第三方框架

热门文章

  1. 【数据结构与算法】之深入解析图的拓扑排序
  2. RuntimeError: Bool type is not supported by dlpack
  3. 2019年第十届蓝桥杯 - 省赛 - C/C++大学B组 - H. 等差数列
  4. 你不知道的Python的输入输出
  5. Go + Excel 学习 Excelize
  6. 计算机视觉之OpenCV教程 --- Mat图像类基础(二)
  7. 【Tools】Visual Studio 2019专业版下载和安装
  8. 【Linux网络编程】套接字的介绍
  9. windos oracle创建sid,window下改oracle_sid | 学步园
  10. 51nod 1557 两个集合 (严谨的逻辑题)