sys_connect_by_path+regexp_substr树状结构,并用正则表达式拆分

依葫芦画瓢git

假若有一张表正则表达式


是  部门           子ID   父ID oracle


一级部门              1    ide


二级部门             2      1函数


三级部门              3       2spa


四级部门              4       3code



个人想法是将部门表转成以下类型regexp


一级部门ID   二级部门ID   三级部门ID  四级部门ID  orm

select s.deptname,
       s.deptcode,
       s.pk_deptdoc,
       sys_connect_by_path(s.pk_deptdoc, '/') aa,
       level,
       sys_connect_by_path(s.deptcode, '/') bb,
     --  case when s.deptcode like '99%' then
    --  rtrim(regexp_substr(sys_connect_by_path(s.pk_deptdoc,'/')||'/','.*?'||'/',1,1),'/')
      -- as hahaha1,
      rtrim(regexp_substr(sys_connect_by_path(s.pk_deptdoc,'/')||'/','.*?'||'/',1,2),'/')
       as hahaha1,
      rtrim(regexp_substr(sys_connect_by_path(s.pk_deptdoc,'/')||'/','.*?'||'/',1,3),'/')
       as hahaha2,
       rtrim(regexp_substr(sys_connect_by_path(s.pk_deptdoc,'/')||'/','.*?'||'/',1,4),'/')
       as hahaha3,
       rtrim(regexp_substr(sys_connect_by_path(s.pk_deptdoc,'/')||'/','.*?'||'/',1,5),'/')
       as hahaha4
     -- end
  from rlzy.tcl_dept s
  where s.deptcode like '99%'
  and (level not in (1,2,3))
 start with s.pk_fathedept = '                    '
connect by prior s.pk_deptdoc = s.pk_fathedept
 order siblings by s.deptcodeci

 

-------

转个正则表达式的文章,之后详细研究下


Oracle 终于在10g中加入了对正则表达式的支持,下面就来了解一下吧!

匹配字符:

字符

功能

^

标记行的开始

$

标记行的结束

= =

匹配字符族,如:[=a=] 可匹配字符a、â、ã、ä、å等

.

匹配任何字符(除了NULL)

[:alnum:]

匹配任何字母和数字

[:alpha:]

匹配任何字母

[:blank:]

匹配空格

[:cntrl:]

匹配任何控制字符(在ASCII码表中不可打印的字符,如ESC键)

[:digit:]

任何数字

[:graph:]

任何 标点符号、大小写字母以及数字

[:lower:]

任何小写字母

[:print:]

任何可打印的字符

[:punct:]

任何标点符号,如,.+-=等

[:space:]

任何空白字符,如回车、换行、制表等

[:upper:]

任何大写字母

[:xdigit:]

任何十六进制数字,即0-9A-F

|

分隔符

()

子表达式,可做为量词或向后引用的前段

[char]

匹配括号中的任何字符,[^char]表示除开括号中字符后的任何字符
 
量词字符:

字符

功能

*

匹配0次或屡次

?

匹配0次或1次

+

匹配1次或屡次

{m}

匹配m次

{m,}

匹配至少m次

{m,n}

匹配至少m次,最多n次

\n

引用第n个匹配

匹配选项: 

字符

功能

c

匹配时区分大小写

i

匹配时不区分大小写

m

源字符串为多行匹配

n

.可匹配任何换行符

函数:
一、REGEXP_LIKE
    是LIKE语句的正则表达式版本
    语法:REGEXP_LIKE(源字符串, 匹配表达式[,匹配选项])
    例子:
     SELECT product_name FROM oe.product_information WHERE regexp_like (product_name, 'SS[PS]/[VS]');
    SS[PS]/[VS]匹配:SSP / V 、SSP / S 、 SSS / V 、 SSS / S 

二、REGEXP_INSTR
    返回源字符串中首次匹配正则表达式的起始位置
    语法:REGEXP_INSTR(srcstr, pattern [, position [, occurrence
               [, return_option [, match_option]]]])
            srcstr:源字符串
            pattern:正则表达式
            position:搜索开始位置
            occurrence:返回第几个匹配项
            return_option:返回选项,0表示开始位置,1表示返回匹配的结束位置
            match_option:匹配选项
    例子:
    SELECT REGEXP_INSTR('500 Oracle Pkwy, Redwood Shores, CA', '[o][[:alpha:]]{3}', 1, 1, 0, 'i') RESULT FROM dual; --返回5
 
    SELECT REGEXP_INSTR('500 Oracle Pkwy, Redwood Shores, CA', '[o][[:alpha:]]{3}', 1, 1, 1, 'i') RESULT FROM dual; --返回9
 
    SELECT REGEXP_INSTR('500 Oracle Pkwy, Redwood Shores, CA', '[o][[:alpha:]]{3}', 1, 2, 0, 'i') RESULT FROM dual; --返回28
 

    SELECT REGEXP_INSTR('500 Oracle Pkwy, Redwood Shores, CA', '[o][[:alpha:]]{3}', 1, 2, 1, 'i') RESULT FROM dual; --返回32

    
    --返回try或trying或tried或tries的位置
    SELECT regexp_instr('We are trying to make the subject easier.',
                      'tr(y(ing)?|(ied)|(ies))') resultnum
    FROM dual;
 
三、REGEXP_SUBSTR
    返回源串中匹配正则表达式的子字符串
    语法:SUBSTR(srcstr, pattern [, position 
                [, occurrence [, match_option]]]) 
            srcstr:源字符串
            pattern:正则表达式
            position:搜索的开始位置
            occurrence:返回第几个匹配的字符串
            match_option:匹配选项
    例子:
    --返回 , Redwood Shores,
    SELECT regexp_substr('500 Oracle Parkway, Redwood Shores, CA', ',[^,]+,') RESULT
  FROM dual;
    
    --返回 http://www.oracle.com/
    SELECT regexp_substr('Go to http://www.oracle.com/products and click on database',
                     'http://([[:alnum:]]+\.?){3,4}/?') RESULT
  FROM dual;
    
    --匹配try或trying或tried或tries
    SELECT regexp_substr('We are trying to make the subject easier.',
                     'tr(y(ing)?|(ied)|(ies))')
  FROM dual;
    
    --返回sidval
    SELECT regexp_substr( 'system/pwd@orabase:1521:sidval', '[^:]+', 1, 3) RESULT FROM dual;
 
    --返回Three
    SELECT regexp_substr('One|Two|Three|Four|Five', '[^|]+', 1, 3) FROM dual;

 
四、REGEXP_REPLACE
    用执行字符串替换源文本中与正则表达式匹配的字符串
    语法:REGEXP_REPLACE(srcstr, pattern [,replacestr [, position
                 [, occurrence [, match_option]]]])

            srcstr:源字符串
            pattern:正则表达式
            replacestr:新的字符串
            position:搜索起始位置
            occurrence:第几个匹配项
            match_option:匹配选项
    例子:
    --返回George Mc Govern

    SELECT regexp_replace('George McGovern', '([[:lower:]])([[:upper:]])', '\1 \2') city


  FROM dual;