oracle 10gRegexREGEXP_LIKE用法

來源:互聯網
上載者:User

標籤:http   io   os   使用   ar   for   資料   sp   c   

偶然需要瞭解,學習了這篇文章,轉載記錄一下

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

ORACLE中的支援Regex的函數主要有下面四個:

1,REGEXP_LIKE :與LIKE的功能相似

2,REGEXP_INSTR :與INSTR的功能相似

3,REGEXP_SUBSTR :與SUBSTR的功能相似

4,REGEXP_REPLACE :與REPLACE的功能相似

它們在用法上與Oracle SQL 函數LIKE、INSTR、SUBSTR 和REPLACE 用法相同,

但是它們使用POSIX Regex代替了老的百分比符號(%)和萬用字元(_)字元。

POSIX Regex由標準的元字元(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:]]‘);

注意:

Regex只是搜尋,替換,格式化等功能,格式化一般用後向引用,沒有計算length和concatenate(串連串聯)的

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

enable/disable對未來的資料有約束/無約束。

validate/novalidate對已有的資料有約束/無約束。

是考欄位約束的,意思是要在表CUSTOMERS的欄位CUST_FIRST_NAME建個約束,使這個欄位不能輸入數字。

類比答案A,以A-Z開頭的,後面可以用數字,這樣就不符合題意!

[email protected]> ALTER TABLE gyj ADD CONSTRAINT cust_f_name CHECK(REGEXP_LIKE(value,‘^A-Z‘)) NOVALIDATE;

Table altered.

[email protected]> insert into gyj values(105,‘A-Z12345‘);

1 row created.

[email protected]> 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

[email protected]> 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數字開頭的,這樣就不符合題意!

[email protected]> alter table gyj drop CONSTRAINT cust_f_name;

Table altered.

[email protected]> ALTER TABLE gyj ADD CONSTRAINT cust_f_name CHECK(REGEXP_LIKE(value,‘^[09]‘)) NOVALIDATE;

Table altered.

[email protected]> insert into gyj values(105,‘09g‘);

1 row created.

[email protected]>

[email protected]> insert into gyj values(105,‘90g‘);

1 row created.

[email protected]> [email protected]> 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:

[email protected]> alter table gyj drop CONSTRAINT cust_f_name;

Table altered.

[email protected]> ALTER TABLE gyj ADD CONSTRAINT cust_f_name CHECK(REGEXP_LIKE(value,‘[[:alpha:]]‘)) NOVALIDATE;

Table altered.

[email protected]> 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

[email protected]> insert into gyj values(105,‘gyj‘);

1 row created.

類比答案D:[[:digit:]] 任何數字,不符合題意!

[email protected]> alter table gyj drop CONSTRAINT cust_f_name;

Table altered.

[email protected]> ALTER TABLE gyj ADD CONSTRAINT cust_f_name CHECK(REGEXP_LIKE(value,‘[[:digit:]]‘)) NOVALIDATE;

Table altered.

[email protected]> insert into gyj values(105,‘1‘);

1 row created.

[email protected]> 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 10gRegexREGEXP_LIKE用法

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.