简介:
参数的含义:
source_string :输入的字符串,可以是列名或者字符串常量、变量。搜索字符串。可以是任意的数据类型char,varchar2,nchar,clob,nclob类型。
pattern:正则表达式元字符构成的匹配模式,长度限制在512字节内。
match_option:匹配选项。取值范围: i:大小写不敏感; c:大小写敏感;n:点号 . 不匹配换行符号;m:多行模式;x:扩展模式,忽略正则表达式中的空白字符。
position:标识从第几个字符开始正则表达式匹配。可选。搜索在字符串中的开始位置。如果省略,默认为1,这是第一个位置的字符串。
occurrence:匹配的次数,标识第几个匹配组。可选。它是模式字符串中的第n个匹配位置。如果省略,默认为1。
return_option: 指定返回的类型,若该参数为0,则返回值为匹配的第一个字符位置,若为非0,则返回匹配值的后一个位置
replace_string:替换的字符串。
一:regexp_like(search_string ,pattern[,match_option])
返回值为一个布尔值。如果第一个参数匹配第二个参数所代表的正则表达式,即找到则返回真,否则将返回假。
二:regexp_instr(search_string,pattern[,position[,occurrence[,return_option[,match_option]]]])
返回找到的匹配字符串的位置。如果查找失败返回0。
三:regexp_substr(search_string,pattern[,position[,occurrence[match_option]]])
返回第一个字符串参数中,与第二个正则表达式参数相匹配的子字符串。查找失败返回为空。
四:regexp_replace(search_string,pattern[,replace_string[,position[,occurrence[match_option]]]])
将source_string中的按pattern匹配到的部分用replace_string代替。
栗子:
regexp_like
select str from tmp where id='like' and regexp_like(str,'A\d+','i'); -- 'i' 忽略大小写
123a34cc
select str from tmp where id='like' and regexp_like(str,'^a\d+$');
a9999
select empno,ename from emp where regexp_like(ename,'^[[:alpha:]]+N$');--查找员工姓名为全英文,且以“N”结尾的员工信息
select empno,ename from emp where regexp_like(empno,'[^[:digit:]]');--查找员工编号以非数字开头的员工信息
regexp_substr
select
str,
regexp_substr(str,'[^,]+') str,
regexp_substr(str,'[^,]+',1,1) str,
regexp_substr(str,'[^,]+',1,2) str, -- occurrence 第几个匹配组
regexp_substr(str,'[^,]+',2,1) str -- position 从第几个字符开始匹配
from tmp
where id='substr';
STR STR STR STR STR
--------------- --------------- --------------- --------------- ---------------
123,234,345 123 123 234 23
12,34.56:78 12 12 34.56:78 2
123456789 123456789 123456789 23456789
regex_instr
select
str,
regexp_instr(str,'\.' ) ind ,
regexp_instr(str,'\.',1,2) ind ,
regexp_instr(str,'\.',5,2) ind
from tmp where id='instr';
STR IND IND IND
--------------- ----- ----- -----
192.168.0.1 4 8 10
regex_replace
select
str,
regexp_replace(str,'020','GZ') str,
regexp_replace(str,'(\d{3})(\d{3})','<\2\1>') str -- 将第一、第二捕获组交换位置,用尖括号标识出来
from tmp
where id='replace';
STR STR STR
--------------- --------------- ---------------
(020)12345678 (GZ)12345678 (020)<456123>78
001517729C28 001517729C28 <517001>729C28
底线