正则表达式regexp_substr截取字符串
①select regexp_substr(string, '[^,]+', 1, level)
from dual
connect by level <= regexp_count(string, '[^,]+')
and rowid = prior (rowid)
and (prior dbms_random.value) is not null;
②select regexp_substr(string, '[^,]+', 1, level)
from dual
connect by level <= length(string) - LENGTH(REPLACE(string, ',', '')) + 1
and rowid = prior (rowid)
and (prior dbms_random.value) is not null;
REGEXP_SUBSTR 函数是把那个串以正则不是以,
(逗号)开头的截取,第二个参数是取第几组,level,connect 循环 ,
循环次数为串总长度-去除分隔符后=几个分隔符 +1
Test1:总结:针对于单行记录来说N1和N2是没有差别的,但是针对于多行记录N1会产生多余数据,那么就通过N2这种方式来处理
N1:
单行记录:
SQL> select regexp_substr('i,am,a,pretty,girl', '[^,]+', 1, level) substring
2 from dual
3 connect by level <= regexp_count('i,am,a,pretty,girl', '[^,]+')
4 ;
SUBSTRING
------------------------------------------------------------------------
i
am
a
pretty
girl
创建一张测试表,针对于多行记录
create table ta as
select 'i,am,a,pretty,girl' substring
from dual
union all
select '1,2,3,4,5,6'
from dual
union all
select '夏,至,未,至'
from dual;
SQL> select count(regexp_substr(substring, '[^,]+', 1, level)) cnt
2 from ta
3 connect by level <= regexp_count(substring, '[^,]+')
4 ;
CNT
----------
444
N2:(解决N1的一种方式 dbms_random.value)
针对于单行记录:
SQL> select regexp_substr('i,am,a,pretty,girl', '[^,]+', 1, level)substring1
2 from dual
3 connect by level <= regexp_count('i,am,a,pretty,girl', '[^,]+')
4 and rowid = prior (rowid)
5 and (prior dbms_random.value) is not null;
SUBSTRING1
------------------------------------------------------------------------
i
am
a
pretty
girl
针对于多行记录
SQL> select count(regexp_substr(substring, '[^,]+', 1, level)) cnt
2 from ta
3 connect by level <= regexp_count(substring, '[^,]+')
4 and rowid = prior (rowid)
5 and (prior dbms_random.value) is not null;
CNT
----------
15
Test2:选取字符串里的数字
SQL> SELECT REGEXP_SUBSTR('1 meeting you,2 waiting for you.7',
2 '[0-9]+',
3 1,
4 level)
5 FROM dual
6 connect by level <=
7 regexp_count('1 meeting you,2 waiting for you.7', '[0-9]+');
REGEXP_SUBSTR('1MEETINGYOU,2WA
--------------------------------------------------------------------------------
1
2
7
Test3:用 REGEP_SUBSTR 函数来查找任意被空格隔开的重复出现的字母数字值
SQL> SELECT REGEXP_SUBSTR('u are best best ', '([[:alnum:]]+)([[:space:]]+)\1') AS substr
2 FROM dual;
SUBSTR
---------
best best
Test4:IP地址前边补0
select listagg(tring1) within group(order by rn) tring
from (select rn, lpad(tring, 3, 0) tring1
from (select level rn,
regexp_substr('12.34.6.110', '[^.]+', 1, level) tring
from dual
connect by level <= 4));
TRING
--------------------------------------------------------------------------------
012034006110