Output: Null
@+ 表示匹配1个或者多个@
[[:alnum:]]+ 表示匹配1个或者多个字母或数字字符
select regexp_substr('@1PSN/231_3253/ABc125','[[:digit:]]+$') from dual;
Output: 125
[[:digit:]]+$ 表示匹配1个或者多个数字结尾的字符
select regexp_substr('@1PSN/231_3253/ABc','[^[:digit:]]+$') from dual;
Output: /ABc
[^[:digit:]]+$ 表示匹配1个或者多个不是数字结尾的字符
select regexp_substr('Tom_Kyte@oracle.com','[^@]+') from dual;
Output: Tom_Kyte
[^@]+ 表示匹配1个或者多个不是“@”的字符
select regexp_substr('1PSN/231_3253/ABc','[[:alnum:]]*',1,2)
from dual;
Output: Null
[[:alnum:]]* 表示匹配0个或者多个字母或者数字字符
注:因为是匹配0个或者多个,所以这里第2次匹配的是“/”(匹配了0次),而不是“231”,所以结果是“Null”
这里我们有时候会查询字符串里asdfafd
Select regexp_substr('asdfafd
Output:
这里我们在<>中间去一个^>这样在匹配<之后,在向后查询的时候确保在匹配到>之前不再在有>,不然的话就要有可以出错的情况。
Select regexp_substr('asdfafd
Output:
在这个例子中,我们在
这个通常用来实现字符串的列传行
select regexp_substr('123;234;345;456;567;678;789','[^;]+',1,rownum) from dual
connect by rownum <= length('123;234;345;456;567;678;789') - length(replace('123;234;345;456;567;678;789',';'))+1
这里length这里操作是先得到有多少个“;”,再通过 connect by rownum方式来做一行成多行的操作,在变成多行之后,可以通过regexp_substr来取字符串的操作
接着上一个例子
a,b,c,d,e,d,f,a,n这样的一个字符串,我们现在要把字符串里一些重复去掉,这样的话结果是a,b,c,d,e,f,n去掉了d与a的两个字符串
select wm_concat(new_row) from (
select distinct regexp_substr('a,b,c,d,e,d,f,a,n','[^,]+',1,rownum) new_row from dual
connect by rownum<=length('a,b,c,d,e,d,f,a,n')-length(replace('a,b,c,d,e,d,f,a,n',',')))
通过转成多行的,再用distinct 去掉重复,然后我们再通过wm_concat来字符串合并来完成。
再来一个ip格式转换的例子吧,我们一般的IP的格式是12.19.168.27现在要不足3位的补足前面为0,结果是012.019.168.027
select wm_concat(new_value) from (
select
lpad(regexp_substr('12.19.168.27','[^.]+',1,rownum) ,3,'0') new_value,rownum
from dual
connect by rownum<5
order by rownum)
来一个验证IP是数字是否正确
select count(*) from(
select
lpad(regexp_substr('12.19.168.27','[^.]+',1,rownum) ,3,'0') new_value,rownum
from dual
connect by rownum<5)
where new_value>=0 and new_value<256
having count(*) =4
来一个IP字符串格式转换成数字型IP
select sum(new_value*power(256,4-rm)) from (
select regexp_substr('12.19.168.27','[^.]+',1,rownum) new_value,rownum rm from dual
connect by rownum<=4
)
接下来介绍一个regexp_instr函数
REGEXP_INSTR 函数使用正则表达式返回搜索模式的起点和终点。REGEXP_INSTR 的语法如下所示。REGEXP_INSTR 返回一个整数,指出搜索模式的开始或结束的位置,如果没有发现匹配的值,则返回0。
语法:
2.REGEXP_INSTR与INSTR函数相同,返回字符串位置
REGEXP_INSTR(srcstr, pattern [, position [, occurrence [, return_option [,match_option]]]])
与REGEXP_SUBSTR一样,它也有变量pattern、position(开始位置)、occurrence 和match_parameter;这里主要介绍一下新参数return_option 的作用,它允许用户告诉Oracle,模式出现的时候,要返回什么内容。
Select regexp_instr('asdfafd
Output:2
这里去查询sd的位置,这个和instr是在相同的
Select regexp_instr('asdfafd
这里是查询da第二出现的位置
还有我们经常会遇到一种情况是,查询某个字段,如果是等于“上海”或“北京”或者我们温州就写成大城市,其它的写成小城市,我们一般会考虑使用decode这种方式
Select decode('上海','上海','大城市','北京' ,'大城市' ,'温州' ,'大城市','小城市') from dual
只有两个我们可能觉的sql也不是很冗长,如果有四五个的话,就有点长了,这里使用regexp_instr就可以很多的去操作
Select decode (regexp_instr('北京','^(上海|北京|温州)'),0,'小城市', '大城市') from dual
通过regexp_instr不匹配时为0的条件,这样就可以完成了
最后一个函数regexp_replace
REGEXP_REPLACE 函数是用另外一个值来替代串中的某个值。例如,可以用一个匹配数字来替代字母的每一次出现。REGEXP_REPLACE的格式如下所示
语法:
4.REGEXP_REPLACE与REPLACE函数相同,替换原字符串中的字符内容
REGEXP_REPLACE(srcstr, pattern [,replacestr [, position [, occurrence [,match_option]]]])
这个替换函数还是一个非常好用的。
如我们在有一个字符串adfadfa (main) next 现在我们要把()替换成<>,这里我们可能想用