oracle 10g正则表达式REGEXP_LIKE用法

系统 2055 0

偶然需要了解,学习了这篇文章,转载记录一下

自:http://www.2cto.com/database/201304/206573.html

ORACLE中的支持正则表达式的函数主要有下面四个:

1,REGEXP_LIKE :与LIKE的功能相似

2,REGEXP_INSTR :与INSTR的功能相似

3,REGEXP_SUBSTR :与SUBSTR的功能相似

4,REGEXP_REPLACE :与REPLACE的功能相似

它们在用法上与Oracle SQL 函数LIKE、INSTR、SUBSTR 和REPLACE 用法相同,

但是它们使用POSIX 正则表达式代替了老的百分号(%)和通配符(_)字符。

POSIX 正则表达式由标准的元字符(metacharacters)所构成:

'^' 匹配输入字符串的开始位置,(注意,若是在方括号表达式中使用^,此时它表示不接受该字符集合,如[^[:digit:]],不是数字)。

'$' 匹配输入字符串的结尾位置。如果设置了 RegExp 对象的 Multiline 属性,则 $ 也匹配 '\n' 或 '\r'。

'.' 匹配除换行符之外的任何单字符。

'?' 匹配前面的子表达式零次或一次。

'+' 匹配前面的子表达式一次或多次。

'*' 匹配前面的子表达式零次或多次。

'|' 指明两项之间的一个选择。例子'^([a-z]+|[0-9]+)$'表示所有小写字母或数字组合成的字符串。

'( )' 标记一个子表达式的开始和结束位置。

'[]' 标记一个中括号表达式。

'{m,n}' 一个精确地出现次数范围,m=<出现次数<=n,'{m}'表示出现m次,'{m,}'表示至少

出现m次。

\num 匹配 num,其中 num 是一个正整数。对所获取的匹配的引用。

字符簇:

[[:alpha:]] 任何字母。

[[:digit:]] 任何数字。

[[:alnum:]] 任何字母和数字。

[[:space:]] 任何白字符。

[[:upper:]] 任何大写字母。

[[:lower:]] 任何小写字母。

[[:punct:]] 任何标点符号。

[[:xdigit:]] 任何16进制的数字,相当于[0-9a-fA-F]。

各种操作符的运算优先级

\转义符

(), (?:), (?=), [] 圆括号和方括号

*, +, ?, {n}, {n,}, {n,m} 限定符

^, $, anymetacharacter 位置和顺序

|

Examples

The following query returns the first and last names for those employees with a first name of Steven or Stephen (wherefirst_name begins withSte and ends with en and in between is eitherv orph):

SELECT first_name, last_name

FROM employees

WHERE REGEXP_LIKE (first_name, '^Ste(v|ph)en$');

FIRST_NAME LAST_NAME

-------------------- -------------------------

Steven King

Steven Markle

Stephen Stiles

 

The following query returns the last name for those employees with a double vowel in their last name (wherelast_name contains two adjacent occurrences of eithera,e, i,o, or u, regardless of case):

SELECT last_name

FROM employees

WHERE REGEXP_LIKE (last_name, '([aeiou])\1', 'i');

LAST_NAME

-------------------------

De Haan

Greenberg

Khoo

Gee

Greene

Lee

Bloom

Feeney

实验测试:

1.创建表

create table gyj (id varchar(4),value varchar(10));

2.数据插入

insert into gyj values ('1','1234560');

insert into gyj values ('2','1234560');

insert into gyj values ('3','1b3b560');

insert into gyj values ('4','abc');

insert into gyj values ('5','abcde');

insert into gyj values ('6','ADREasx');

insert into gyj values ('7','123 45');

insert into gyj values ('8','adc de');

insert into gyj values ('9','adc,.de');

insert into gyj values ('10','1B');

insert into gyj values ('10','abcbvbnb');

insert into gyj values ('11','11114560');

insert into gyj values ('11','11124560');

commit;

3.regexp_like

--查询value中以1开头60结束的记录并且长度是7位

select * from gyj where value like '1____60';

select * from gyj where regexp_like(value,'1....60');

--查询value中以1开头60结束的记录并且长度是7位并且全部是数字的记录。

--使用like就不是很好实现了。

select * from gyj where regexp_like(value,'1[0-9]{4}60');

-- 也可以这样实现,使用字符集。

select * from gyj where regexp_like(value,'1[[:digit:]]{4}60');

-- 查询value中不是纯数字的记录

select * from gyj where not regexp_like(value,'^[[:digit:]]+$');

-- 查询value中不包含任何数字的记录。

select * from gyj where regexp_like(value,'^[^[:digit:]]+$');

--查询以12或者1b开头的记录.不区分大小写。

select * from gyj where regexp_like(value,'^1[2b]','i');

--查询以12或者1b开头的记录.区分大小写。

select * from gyj where regexp_like(value,'^1[2B]');

-- 查询数据中包含空白的记录。

select * from gyj where regexp_like(value,'[[:space:]]');

--查询所有包含小写字母或者数字的记录。

select * from gyj where regexp_like(value,'^([a-z]+|[0-9]+)$');

--查询任何包含标点符号的记录。

select * from gyj where regexp_like(value,'[[:punct:]]');

注意:

正则表达式只是搜索,替换,格式化等功能,格式化一般用后向引用,没有计算length和concatenate(连接串联)的

************************************************************************

enable/disable对未来的数据有约束/无约束。

validate/novalidate对已有的数据有约束/无约束。

是考字段约束的,意思是要在表CUSTOMERS的字段CUST_FIRST_NAME建个约束,使这个字段不能输入数字。

模拟答案A,以A-Z开头的,后面可以用数字,这样就不符合题意!

gyj@OCM> ALTER TABLE gyj ADD CONSTRAINT cust_f_name CHECK(REGEXP_LIKE(value,'^A-Z')) NOVALIDATE;

Table altered.

gyj@OCM> insert into gyj values(105,'A-Z12345');

1 row created.

gyj@OCM> insert into gyj values(105,'-AZ12345');

insert into gyj values(105,'-AZ12345')

*

ERROR at line 1:

ORA-02290: check constraint (GYJ.CUST_F_NAME) violated

gyj@OCM> insert into gyj values(105,'Z-A12345');

insert into gyj values(105,'Z-A12345')

*

ERROR at line 1:

ORA-02290: check constraint (GYJ.CUST_F_NAME) violated

模拟答案B:以0或9数字开头的,这样就不符合题意!

gyj@OCM> alter table gyj drop CONSTRAINT cust_f_name;

Table altered.

gyj@OCM> ALTER TABLE gyj ADD CONSTRAINT cust_f_name CHECK(REGEXP_LIKE(value,'^[09]')) NOVALIDATE;

Table altered.

gyj@OCM> insert into gyj values(105,'09g');

1 row created.

gyj@OCM>

gyj@OCM> insert into gyj values(105,'90g');

1 row created.

gyj@OCM> gyj@OCM> insert into gyj values(105,'190g');

insert into gyj values(105,'190g')

*

ERROR at line 1:

ORA-02290: check constraint (GYJ.CUST_F_NAME) violated

模拟体答案C:

gyj@OCM> alter table gyj drop CONSTRAINT cust_f_name;

Table altered.

gyj@OCM> ALTER TABLE gyj ADD CONSTRAINT cust_f_name CHECK(REGEXP_LIKE(value,'[[:alpha:]]')) NOVALIDATE;

Table altered.

gyj@OCM> insert into gyj values(105,'1');

insert into gyj values(105,'1')

*

ERROR at line 1:

ORA-02290: check constraint (GYJ.CUST_F_NAME) violated

gyj@OCM> insert into gyj values(105,'gyj');

1 row created.

模拟答案D:[[:digit:]] 任何数字,不符合题意!

gyj@OCM> alter table gyj drop CONSTRAINT cust_f_name;

Table altered.

gyj@OCM> ALTER TABLE gyj ADD CONSTRAINT cust_f_name CHECK(REGEXP_LIKE(value,'[[:digit:]]')) NOVALIDATE;

Table altered.

gyj@OCM> insert into gyj values(105,'1');

1 row created.

gyj@OCM> insert into gyj values(105,'gyj');

insert into gyj values(105,'gyj')

*

ERROR at line 1:

ORA-02290: check constraint (GYJ.CUST_F_NAME) violated

oracle 10g正则表达式REGEXP_LIKE用法


更多文章、技术交流、商务合作、联系博主

微信扫码或搜索:z360901061

微信扫一扫加我为好友

QQ号联系: 360901061

您的支持是博主写作最大的动力,如果您喜欢我的文章,感觉我的文章对您有帮助,请用微信扫描下面二维码支持博主2元、5元、10元、20元等您想捐的金额吧,狠狠点击下面给点支持吧,站长非常感激您!手机微信长按不能支付解决办法:请将微信支付二维码保存到相册,切换到微信,然后点击微信右上角扫一扫功能,选择支付二维码完成支付。

【本文对您有帮助就好】

您的支持是博主写作最大的动力,如果您喜欢我的文章,感觉我的文章对您有帮助,请用微信扫描上面二维码支持博主2元、5元、10元、自定义金额等您想捐的金额吧,站长会非常 感谢您的哦!!!

发表我的评论
最新评论 总共0条评论