http://blog.csdn.net/guoyjoe/article/details/8852184
Oracle 中支持正则表达式函数有下面四个
1:REGEXP_LIKE 与 LIKE功能相似
2:REGEXP_INSTR
3:REGEXP_SUBSTR
4:REGEXP_REPLACE
用法相同但是它们使用POSIX
正则表达式代替了老百分号 % _
^ 开始
$ 结束
. 除换行外的任何单字符
? 零次1次
+ 一次到多次
* 零次到多次
| 两项之一选项
() 子表达式开始结束
[] 中括号表达式
{m,n} 范围
[[:alpha:]] 任何字母
[[:digit:]] 任何数字
[[:alnum:]] 任何字母和数字
[[:space:]] 任何空白字符
[[:upper:]] 任何大写字母
[[:lower:]] 任何小写字母
[[:punct:]] 任何标点符号
[[:xdigit:]] 任何16进制数字
1创建表
create table tom(id varchar(4),value varchar(20));
2:数据插入
insert into tom values('1','1234560');
insert into tom values('2','1234560');
insert into tom values('3','1b3b560');
insert into tom values('4','abc');
insert into tom values('5','abcde');
insert into tom values('6','ADREasx');
insert into tom values('7','123 45');
insert into tom values('8','adc de');
insert into tom values('9','ade,.de');
insert into tom values('10','1B');
insert into tom values('10','abcdvdnb');
insert into tom values('11','11114560');
insert into tom values('11','111245560');
3:regexp_like
-查询value中以1开头60结束的记录并且长度是7位
select * from tom where value like '1____60';
select * from tom where regexp_like(value,'^1....60$'); //必须严格匹配
-查询value中以1开头60结尾并且长度是7位并且全部数字的记录
select * from tom where regexp_like(value,'^1[0-9]{4}60$');
-也可以这样实现使用字符集
select * from tom where regexp_like(value,'^1[[:digit:]]{4}60$');
-查询value中不是纯数字的记录
select * from tom where regexp_like(value,'^[^[:digit:]]+$');
-查询以 12 或者 1b开头的记录不区分大小写
select * from tom where regexp_like(value,'^1[2b]','i');
-查询以 12 或者 1b开头的记录区分大小写
select * from tom where regexp_like(value,'^1[2B]');
-查询数据中包含空白的记录
select * from tom where regexp_like(value,'s+');
select * from tom where regexp_like(value,'[[:space:]]');
-查询所有包含小写字母或数字的记录
select * from tom where regexp_like(value,'[a-z0-9]+');
select * from tom where regexp_like(value,'[a-z]+|[0-9]+');
-查询任何包含标点符号的记录
select * from tom where regexp_like(value,'[[:punct:]]');
注意:
正则表达式只是搜索,替换,格式化等功能..
添加索引
ALTER TABLE tom ADD CONSTRAINT cust_f_name CHECK(REGEXP_LIKE(VALUE,'^A-Z')) NOVALIDATE;
INSERT INTO TOM VALUES(105,'A-Z12345');
INSERT INTO TOM VALUES(106,'-AZ123345');
*
第 1 行出现错误:
ORA-02290: 违反检查约束条件 (SYS.CUST_F_NAME)
insert into tom VALUES(107,'Z-A123345');
*
第 1 行出现错误:
ORA-02290: 违反检查约束条件 (SYS.CUST_F_NAME)
删除约束
ALTER TABLE tom DROP CONSTRAINT cust_f_name;
ALTER TABLE tom ADD CONSTRAINT cust_f_name CHECK(REGEXP_LIKE(value,'^[0-9]')) NOVALIDATE;
INSERT INTO tom values(105,'09G');
insert into tom values(105,'190g');