偶然需要了解,學習了這篇文章,轉(zhuǎn)載記錄一下
自:http://www.2cto.com/database/201304/206573.html
ORACLE中的支持正則表達式的函數(shù)主要有下面四個:
1,REGEXP_LIKE :與LIKE的功能相似
2,REGEXP_INSTR :與INSTR的功能相似
3,REGEXP_SUBSTR :與SUBSTR的功能相似
4,REGEXP_REPLACE :與REPLACE的功能相似
它們在用法上與Oracle SQL 函數(shù)LIKE、INSTR、SUBSTR 和REPLACE 用法相同,
但是它們使用POSIX 正則表達式代替了老的百分號(%)和通配符(_)字符。
POSIX 正則表達式由標準的元字符(metacharacters)所構(gòu)成:
'^' 匹配輸入字符串的開始位置,(注意,若是在方括號表達式中使用^,此時它表示不接受該字符集合,如[^[:digit:]],不是數(shù)字)。
'$' 匹配輸入字符串的結(jié)尾位置。如果設(shè)置了 RegExp 對象的 Multiline 屬性,則 $ 也匹配 '\n' 或 '\r'。
'.' 匹配除換行符之外的任何單字符。
'?' 匹配前面的子表達式零次或一次。
'+' 匹配前面的子表達式一次或多次。
'*' 匹配前面的子表達式零次或多次。
'|' 指明兩項之間的一個選擇。例子'^([a-z]+|[0-9]+)$'表示所有小寫字母或數(shù)字組合成的字符串。
'( )' 標記一個子表達式的開始和結(jié)束位置。
'[]' 標記一個中括號表達式。
'{m,n}' 一個精確地出現(xiàn)次數(shù)范圍,m=<出現(xiàn)次數(shù)<=n,'{m}'表示出現(xiàn)m次,'{m,}'表示至少
出現(xiàn)m次。
\num 匹配 num,其中 num 是一個正整數(shù)。對所獲取的匹配的引用。
字符簇:
[[:alpha:]] 任何字母。
[[:digit:]] 任何數(shù)字。
[[:alnum:]] 任何字母和數(shù)字。
[[:space:]] 任何白字符。
[[:upper:]] 任何大寫字母。
[[:lower:]] 任何小寫字母。
[[:punct:]] 任何標點符號。
[[:xdigit:]] 任何16進制的數(shù)字,相當于[0-9a-fA-F]。
各種操作符的運算優(yōu)先級
\轉(zhuǎn)義符
(), (?:), (?=), [] 圓括號和方括號
*, +, ?, {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.創(chuàng)建表
create table gyj (id varchar(4),value varchar(10));
2.數(shù)據(jù)插入
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結(jié)束的記錄并且長度是7位
select * from gyj where value like '1____60';
select * from gyj where regexp_like(value,'1....60');
--查詢value中以1開頭60結(jié)束的記錄并且長度是7位并且全部是數(shù)字的記錄。
--使用like就不是很好實現(xiàn)了。
select * from gyj where regexp_like(value,'1[0-9]{4}60');
-- 也可以這樣實現(xiàn),使用字符集。
select * from gyj where regexp_like(value,'1[[:digit:]]{4}60');
-- 查詢value中不是純數(shù)字的記錄
select * from gyj where not regexp_like(value,'^[[:digit:]]+$');
-- 查詢value中不包含任何數(shù)字的記錄。
select * from gyj where regexp_like(value,'^[^[:digit:]]+$');
--查詢以12或者1b開頭的記錄.不區(qū)分大小寫。
select * from gyj where regexp_like(value,'^1[2b]','i');
--查詢以12或者1b開頭的記錄.區(qū)分大小寫。
select * from gyj where regexp_like(value,'^1[2B]');
-- 查詢數(shù)據(jù)中包含空白的記錄。
select * from gyj where regexp_like(value,'[[:space:]]');
--查詢所有包含小寫字母或者數(shù)字的記錄。
select * from gyj where regexp_like(value,'^([a-z]+|[0-9]+)$');
--查詢?nèi)魏伟瑯它c符號的記錄。
select * from gyj where regexp_like(value,'[[:punct:]]');
注意:
正則表達式只是搜索,替換,格式化等功能,格式化一般用后向引用,沒有計算length和concatenate(連接串聯(lián))的
************************************************************************
enable/disable對未來的數(shù)據(jù)有約束/無約束。
validate/novalidate對已有的數(shù)據(jù)有約束/無約束。
是考字段約束的,意思是要在表CUSTOMERS的字段CUST_FIRST_NAME建個約束,使這個字段不能輸入數(shù)字。
模擬答案A,以A-Z開頭的,后面可以用數(shù)字,這樣就不符合題意!
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數(shù)字開頭的,這樣就不符合題意!
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:]] 任何數(shù)字,不符合題意!
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
更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號聯(lián)系: 360901061
您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對您有幫助就好】元
