[每日一题] OCP1z0-047 :2013-07-13 oracle 10g正则表达式 REGEXP_LIKE 用法.....................................2

转载请注明出处:http://blog.csdn.net/guoyjoe/article/details/8852184





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,ei,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




**********本博客所有内容均为原创,如有转载请注明作者和出处!!!**********
Name:    guoyJoe

QQ:      252803295

Email:    oracledba_cn@hotmail.com

Blog:      http://blog.csdn.net/guoyJoe

ITPUB:   http://www.itpub.net/space-uid-28460966.html

OCM:    http://education.oracle.com/education/otn/YGuo.HTM
_____________________________________________________________
加群验证问题:哪些SGA结构是必需的,哪些是可选的?否则拒绝申请!!!

答案在:http://blog.csdn.net/guoyjoe/article/details/8624392

DSI&Core Search(QQ群):127149411


  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 4
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值