1、regexp_like()
create table exp_test(
id varchar2(4),
value varchar2(10)
);
SQL> insert into exp_test values('1','1234560');
1 row inserted
SQL> insert into exp_test values('2','1234560');
1 row inserted
SQL> insert into exp_test values('3','1b3b560');
1 row inserted
SQL> insert into exp_test values('4','abc');
1 row inserted
SQL> insert into exp_test values('5','abcde');
1 row inserted
SQL> insert into exp_test values('6','ADREasx');
1 row inserted
SQL> insert into exp_test values('7','123 45');
1 row inserted
SQL> insert into exp_test values('8','abc de');
1 row inserted
SQL> insert into exp_test values('9','abc,.de');
1 row inserted
SQL> insert into exp_test values('10','abcbvbnb');
1 row inserted
SQL> insert into exp_test values('11','11114560');
1 row inserted
SQL> insert into exp_test values('11','11124560');
1 row inserted
.......................................
SQL> commit;
SQL> select * from exp_test;
ID VALUE
---- ----------
12 1256760
14 12345360
1 1234560
2 1234560
3 1b3b560
4 abc
5 abcde
6 ADREasx
7 123 45
8 abc de
9 abc,.de
10 abcbvbnb
11 11114560
11 11124560
--查询 value 中以 1开头 60 结束的记录并且长度是 7位
SQL> select * from exp_test where regexp_like(value,'^1....60') order by id desc;
ID VALUE
---- ----------
3 1b3b560
2 1234560
12 1256760
1 1234560
SQL> select * from exp_test where value like '1____60';
ID VALUE
---- ----------
12 1256760
1 1234560
2 1234560
3 1b3b560
SQL> select * from exp_test where regexp_like(value,'1....60') ;----这种是不对的
ID VALUE
---- ----------
12 1256760
1 1234560
2 1234560
3 1b3b560
11 11114560
11 11124560
--查询 value 中以 1开头 60 结束的记录并且长度是 7位并且全部是数字的记录。
SQL> select * from exp_test where regexp_like(value,'^1[[:digit:]]{4}60');
ID VALUE
---- ----------
12 1256760
1 1234560
2 1234560
SQL> select * from exp_test where regexp_like(value,'^1[0-9]{4}60');
ID VALUE
---- ----------
12 1256760
1 1234560
2 1234560
-- 查询 value 中不是纯数字的记录
SQL> select * from exp_test where not regexp_like(value,'^[0-9]+$');
ID VALUE
---- ----------
3 1b3b560
4 abc
5 abcde
6 ADREasx
7 123 45
8 abc de
9 abc,.de
10 abcbvbnb
8 rows selected
SQL> select * from exp_test where not regexp_like(value,'^[[:digit:]]+$');
ID VALUE
---- ----------
3 1b3b560
4 abc
5 abcde
6 ADREasx
7 123 45
8 abc de
9 abc,.de
10 abcbvbnb
8 rows selected
-- 查询 value 中不包含任何数字的记录。
SQL> select * from exp_test where regexp_like(value,'^[^[:digit:]]+$');
ID VALUE
---- ----------
4 abc
5 abcde
6 ADREasx
8 abc de
9 abc,.de
10 abcbvbnb
6 rows selected
--查询以 12 或者 1b 开头的记录.不区分大小写。
SQL> select * from exp_test where regexp_like(value,'^1[2B]','i');
ID VALUE
---- ----------
12 1256760
14 12345360
1 1234560
2 1234560
3 1b3b560
7 123 45
01 1Be4f555
7 rows selected
--查询以 12 或者 1b 开头的记录.区分大小写。
SQL> select * from exp_test where regexp_like(value,'^1[2b]');
ID VALUE
---- ----------
12 1256760
14 12345360
1 1234560
2 1234560
3 1b3b560
7 123 45
6 rows selected
SQL> select * from exp_test where regexp_like(value,'^1[2B]');
ID VALUE
---- ----------
12 1256760
14 12345360
1 1234560
2 1234560
7 123 45
01 1Be4f555
-- 查询数据中包含空白的记录。
SQL> select * from exp_test where regexp_like(value,'[[:space:]]');
ID VALUE
---- ----------
7 123 45
8 abc de
--查询所有包含小写字母或者数字的记录。
SQL> select * from exp_test where regexp_like(value ,'[0-9]|[[:lower:]]');
ID VALUE
---- ----------
12 1256760
14 12345360
1 1234560
2 1234560
3 1b3b560
4 abc
5 abcde
6 ADREasx
7 123 45
8 abc de
9 abc,.de
10 abcbvbnb
11 11114560
11 11124560
01 1Be4f555
15 rows selected
SQL> select * from exp_test where regexp_like(value ,'[0-9]|[a-z]');
ID VALUE
---- ----------
12 1256760
14 12345360
1 1234560
2 1234560
3 1b3b560
4 abc
5 abcde
6 ADREasx
7 123 45
8 abc de
9 abc,.de
10 abcbvbnb
11 11114560
11 11124560
01 1Be4f555
15 rows selected
2、regexp_replace()
SQL> select regexp_replace('09/29/2008', '^([0-9]{2})/([0-9]{2})/([0-9]{4})$', '3-1-2') replace from dual;
REPLACE
----------
2008-09-29
num 匹配 num,其中 num 是一个正整数。对所获取的匹配的引用。
3、regexp_instr()
regexp_instr()与instr()用法类似
SQL> select regexp_instr(value,'a',1,1) ,value,instr(value,'a',1,1) from exp_test;
REGEXP_INSTR(VALUE,'A',1,1) VALUE INSTR(VALUE,'A',1,1)
--------------------------- ---------- --------------------
0 1256760 0
0 12345360 0
0 1234560 0
0 1234560 0
0 1b3b560 0
1 abc 1
1 abcde 1
5 ADREasx 5
0 123 45 0
1 abc de 1
1 abc,.de 1
1 abcbvbnb 1
0 11114560 0
0 11124560 0
0 1Be4f555 0
15 rows selected
4、regexp_substr()
regexp_substr(expression, regexp)。
表html中存储了HTML标签及内容。现欲从标签中获得
链接的url。
SQL> select * from html;
ID HTML
----- ----------- ----------------------------------
1 maillinkhttp://mail.google.com/2009/1009.html">maillink>
SQL> select id, regexp_substr(html, 'http[a-zA-Z0-9.:/]*') url from html;
ID URL
------------ ---------------------
1 http://mail.google.com/2009/1009.html
比较regexp_substr() 与substr()
SQL> select regexp_substr(value,'[0-9]{1}',1,2) ,substr(value,1,3) ,value from exp_test;
REGEXP_SUBSTR(VALUE,'[0-9]{1}' SUBSTR(VALUE,1,3) VALUE
------------------------------ ----------------- ----------
2 125 1256760
2 123 12345360
2 123 1234560
2 123 1234560
3 1b3 1b3b560
abc abc
abc abcde
ADR ADREasx
2 123 123 45
abc abc de
abc abc,.de
abc abcbvbnb
1 111 11114560
1 111 11124560
4 1Be 1Be4f555
15 rows selected
SQL> select regexp_substr(value,'[0-9]+') ,substr(value,1,3) ,value from exp_test;
REGEXP_SUBSTR(VALUE,'[0-9]+') SUBSTR(VALUE,1,3) VALUE
------------------------------ ----------------- ----------
1256760 125 1256760
12345360 123 12345360
1234560 123 1234560
1234560 123 1234560
1 1b3 1b3b560
abc abc
abc abcde
ADR ADREasx
123 123 123 45
abc abc de
abc abc,.de
abc abcbvbnb
11114560 111 11114560
11124560 111 11124560
1 1Be 1Be4f555
15 rows selected
来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/25101849/viewspace-1057653/,如需转载,请注明出处,否则将追究法律责任。
转载于:http://blog.itpub.net/25101849/viewspace-1057653/