36.Oracle数据库SQL开发之 使用简单函数——使用单行函数正则表达式函数
欢迎转载,转载请标明出处:blog.csdn.net/notbaron/ar…\
这些函数可以在字符串中搜索字符模式。
1. REGEXP_LIKE()
REGEXP_LIKE(x,pattern [,match_option])用于在x中查找pattern参数中定义的正则表达式。
可以设置为下面几个字符之一:
‘c’,说明在匹配时区分大小写
‘I’,说明在匹配时不区分大小写
‘n’,允许使用可以匹配任意字符的操作符
‘m’,将x作为一个包含多行的字符串
store@PDB1> select customer_id,first_name,last_name,dobfrom customers where regexp_like(to_char(dob,'YYYY'),'^196[5-8]$');
CUSTOMER_ID FIRST_NAME LAST_NAME DOB
----------- ---------- ---------- ---------
1 John Brown 01-JAN-65
2 Cynthia Green 05-FEB-68
检索名字以J或j开头的顾客。
例如:
store@PDB1> selectcustomer_id,first_name,last_name,dob from customers whereregexp_like(first_name,'^j','i');
CUSTOMER_ID FIRST_NAME LAST_NAME DOB
----------- ---------- ---------- ---------
1 John Brown 01-JAN-65
2. REGEXP_INSTR()
REGEXP_INSTR(x,pattern [,start [,occurrence[,return_option [, match_option]]]]) 用于在x中查找pattern,返回pattern出现的位置。
例如:
store@PDB1> select regexp_instr('But,soft! Whatlight through yonder window breaks?', 'l[[:alpha:]]{4}') as result from dual;
RESULT
----------
16
store@PDB1> select regexp_instr('But, soft! Whatlight through yonder window breaks?','o',10,2) as result from dual;
RESULT
----------
32
3. REGEXP_REPLACE()
REGEXP_REPLACE(x,pattern [,replace_string[,start [,occurrence[, match_option]]]])用于在x 中查找pattern,将其替换为replace_string.
例如:
store@PDB1> select regexp_replace('But, soft! Whatlight through yonder window breaks?','l[[:alpha:]]{4}','sound') as result fromdual;
RESULT
---------------------------------------------------
But, soft! What sound through yonder windowbreaks?
4. REGEXP_SUBSTR()
REGEXP_SUBSTR(x,pattern[, start [, occurrence[, match_option]]]) 用于在x中查找匹配pattern的字符串,开始位置由start指定。
store@PDB1> select regexp_substr('But, soft! Whatlight through yonder window breaks?','l[[:alpha:]]{4}') as result from dual;
RESUL
-----
light
5. REGEXP_COUNT()
REGEXP_COUNT()是11g新增加的一个函数。
REGEXP_COUNT(x,pattern[, start[,match_option]])用于在x中查找pattern,并返回pattern在x中出现的次数。
例如:
store@PDB1> select regexp_count('But, soft! Whatlight through yonder window softly breaks?','s[[:alpha:]]{3}') as result fromdual;
RESULT
----------
2