sys_connect_by_path+regexp_substr树状结构,并用正则表达式拆分
2010-12-09 15:17
239 查看
依葫芦画瓢
假如有一张表
是 部门 子ID 父ID
一级部门 1
二级部门 2 1
三级部门 3 2
四级部门 4 3
我的想法是将部门表转成如下类型
一级部门ID 二级部门ID 三级部门ID 四级部门ID
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.deptcode
-------
转个正则表达式的文章,以后详细研究下
Oracle 终于在10g中加入了对正则表达式的支持,下面就来了解一下吧!
匹配字符:
量词字符:
匹配选项:
函数:
1、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
2、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;
3、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;
4、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;
假如有一张表
是 部门 子ID 父ID
一级部门 1
二级部门 2 1
三级部门 3 2
四级部门 4 3
我的想法是将部门表转成如下类型
一级部门ID 二级部门ID 三级部门ID 四级部门ID
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.deptcode
-------
转个正则表达式的文章,以后详细研究下
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 | .可匹配任何换行符 |
1、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
2、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;
3、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;
4、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;
相关文章推荐
- oracle 使用REGEXP_SUBSTR正则表达式拆分字符串
- oracle正则表达式函数之REGEXP_REPLACE和REGEXP_SUBSTR
- sys_connect_by_path的用法 简单的树型结构
- Oracle正则表达式函数:regexp_like、regexp_substr、regexp_instr、regexp_replace
- Oracle正则表达式函数:regexp_like、regexp_substr、regexp_instr、regexp_replace(转载保存一下)
- Oracle正则表达式函数:regexp_like、regexp_substr、regexp_instr、regexp_replace
- 树结构和它的专用函数SYS_CONNECT_BY_PATH
- Oracle正则表达式函数:regexp_like、regexp_substr、regexp_instr、regexp_replace
- Oracle正则表达式函数:regexp_like、regexp_substr、regexp_instr、regexp_replace
- oracle正则表达式函数之REGEXP_REPLACE和REGEXP_SUBSTR
- Oracle 正则表达式函数-REGEXP_SUBSTR 使用例子
- Oracle正则表达式函数:regexp_like、regexp_substr、regexp_instr、regexp_replace
- Oracle正则表达式函数:regexp_like、regexp_substr、regexp_instr、regexp_replace
- 树结构和它的专用函数SYS_CONNECT_BY_PATH
- Oracle中REGEXP_SUBSTR及其它支持正则表达式的内置函数小结
- Oracle正则表达式函数:regexp_like、regexp_substr、regexp_instr、regexp_replace
- oracle regexp_substr 报ora-12725 正则表达式中的圆括号不配对
- Oracle正则表达式之 Regexp_substr,Regexp_instr,Regexp_replace,Regexp_like
- Oracle正则表达式函数:regexp_like、regexp_substr、regexp_instr、regexp_replace
- 数据库树型结构中获取目录串的高性能方法,sys_connect_by_path与自定义函数性能对比