1. 程式人生 > >oracle正則表達式

oracle正則表達式

single 之間 ood 優先級 能力 方括號 如果 ash including

oracle的正則表達式(regular expression)

特殊字符:
‘^‘ 匹配輸入字符串的開始位置,在方括號表達式中使用,此時它表示不接受該字符集合。
‘$‘ 匹配輸入字符串的結尾位置。如果設置了 RegExp 對象的 Multiline 屬性,則 $ 也匹配 ‘n‘ 或 ‘r‘。
‘.‘ 匹配除換行符 n之外的任何單字符。
‘?‘ 匹配前面的子表達式零次或一次。
‘*‘ 匹配前面的子表達式零次或多次。
‘+‘ 匹配前面的子表達式一次或多次。
‘( )‘ 標記一個子表達式的開始和結束位置。
‘[]‘ 標記一個中括號表達式。
‘{m,n}‘ 一個精確地出現次數範圍,m= <出現次數 <=n,‘{m}‘表示出現m次,‘{m,}‘表示至少出現m次。
‘ |‘ 指明兩項之間的一個選擇。例子‘^([a-z]+ |[0-9]+)$‘表示所有小寫字母或數字組合成的字符串。
num 匹配 num,其中 num 是一個正整數。對所獲取的匹配的引用。
正則表達式的一個很有用的特點是可以保存子表達式以後使用, 被稱為Backreferencing. 允許復雜的替換能力
如調整一個模式到新的位置或者指示被代替的字符或者單詞的位置. 被匹配的子表達式存儲在臨時緩沖區中,
緩沖區從左到右編號, 通過數字符號訪問。 下面的例子列出了把名字 aa bb cc 變成
cc, bb, aa.
Select REGEXP_REPLACE(‘aa bb cc‘,‘(.*) (.*) (.*)‘, ‘3, 2, 1‘) FROM dual;

字符簇:


[[:alpha:]] 任何字母。
[[:digit:]] 任何數字。
[[:alnum:]] 任何字母和數字。
[[:space:]] 任何白字符。
[[:upper:]] 任何大寫字母。
[[:lower:]] 任何小寫字母。
[[:punct:]] 任何標點符號。
[[:xdigit:]] 任何16進制的數字,相當於[0-9a-fA-F]
各種操作符的運算優先級
(), (?:), (?=), [] 圓括號和方括號
*, +, ?, {n}, {n,}, {n,m} 限定符
^, $, anymetacharacter 位置和順序
| “或”操作

match_option的取值如下:
‘c’ 說明在進行匹配時區分大小寫(缺省值);
x^I|*I0 ‘i‘ 說明在進行匹配時不區分大小寫;
‘n‘ 允許使用可以匹配任意字符的操作符;
‘m‘ 將x作為一個包含多行的字符串。

. 匹配字符

字符類

匹配的字符

\d

從0-9的任一數字

\d\d匹配72,但不匹配aa或7a

\D

任一非數字字符

\D\D\D匹配abc,但不匹配123

\w

任一單詞字符,包括A-Z,a-z,0-9和下劃線

\w\w\w\w匹配Ab-2,但不匹配∑£$%*或Ab_@

\W

任一非單詞字符

\W匹配@,但不匹配a

\s

任一空白字符,包括制表符,換行符,回車符,換頁符和垂直制表符

匹配在HTML,XML和其他標準定義中的所有傳統空白字符

\S

任一非空白字符

空白字符以外的任意字符,如A%&g3;等

.

任一字符

匹配除換行符以外的任意字符除非設置了MultiLine先項

[…]

括號中的任一字符

[abc]將匹配一個單字符,a,b或c.

[a-z]將匹配從a到z的任一字符

[^…]

不在括號中的任一字符

[^abc]將匹配一個a、b、c之外的單字符,可以a,b或A、B、C

[a-z]將匹配不屬於a-z的任一字符,但可以匹配所有的大寫字母

. 重復字符

重復字符

{n}

匹配前面的字符n次

x{2}匹配xx,但不匹配x或xxx

{n,}

匹配前面的字符至少n次

x{2}匹配2個或更多的x,如xxx,xxx..

{n,m}

匹配前面的字符至少n次,至多m次。如果n為0,此參數為可選參數

x{2,4}匹配xx,xxx,xxxx,但不匹配xxxxx

?

匹配前面的字符0次或1次,實質上也是可選的

x?匹配x或零個x

+

匹配前面的字符0次或多次

x+匹配x或xx或大於0的任意多個x

*

匹配前面的字符0次或更多次

x*匹配0,1或更多個x

. 定位字符

定位字符

^

隨後的模式必須位於字符串的開始位置,如果是一個多行字符串,則必須位於行首。對於多行文本(包含回車符的一個字符串)來說,需要設置多行標誌

$

前面的模式必須位於字符串的未端,如果是一個多行字符串,必須位於行尾

\A

前面的模式必須位於字符串的開始位置,忽略多行標誌

\z

前面的模式必須位於字符串的未端,忽略多行標誌

\Z

前面的模式必須位於字符串的未端,或者位於一個換行符前

\b

匹配一個單詞邊界,也就是一個單詞字符和非單詞字符中間的點。要記住一個單詞字符是[a-zA-Z0-9]中的一個字符。位於一個單詞的詞首

\B

匹配一個非單詞字符邊界位置,不是一個單詞的詞首

REGEXP_LIKE(source_string, pattern[, match_parameter])函數

技術分享圖片

描述:返回滿足匹配模式的字符串。相當於增強的like函數。

Source_string指定源字符表達式;

pattern指定規則表達式;

match_parameter指定默認匹配操作的文本串。

Examples

--The following query returns the first and last names for those employees with a first name of Steven or Stephen 
--(where first_name begins with Ste and ends with en and in between is either v or ph):
--查詢返回結果為first_name以ste開頭,en結尾,中間為v或ph
SQL> SELECT first_name, last_name
  2  FROM employees
  3  WHERE REGEXP_LIKE (first_name, ^Ste(v|ph)en$)
  4  ORDER BY first_name, last_name;
FIRST_NAME           LAST_NAME
-------------------- -------------------------
Stephen              Stiles
Steven               King
Steven               Markle

--The following query returns the last name for those employees with a double vowel in their last name 
--(where last_name contains two adjacent occurrences of either a, e, i, o, or u, regardless of case):
--last_name中包含aeiou中任何一個,不區分大小寫
SELECT last_name
FROM employees
WHERE REGEXP_LIKE (last_name, ([aeiou])\1, i)
ORDER BY last_name;

LAST_NAME
-------------------------
De Haan
Greenberg
Khoo
Gee
Greene
Lee
Bloom
Feeney

--用到的實例 判斷客戶信息中手機號格式是否正確
   REGEXP_LIKE(CUS_MOBILE1, \d*([0-9]{1}+[0-9]{1}+)\1{4,}\d*) --類似1313131……
OR REGEXP_LIKE(CUS_MOBILE1, \d*(\d)\1{6,}\d*) --某一位數連續重復5次 OR REGEXP_LIKE(CUS_MOBILE1,0123|1234|2345|3456|4567|5678|6789|7890|01234|12345|23456|34567|45678|56789|67890
|012345|123456|234567|345678|456789|567890
|0123456|1234567|2345678|3456789|4567890|01234567|12345678|23456789|34567890
|012345678|123456789|234567890|0123456789|1234567890|
) --連續增長7位以上

REGEXP_REPLACE(source_string,pattern,replace_string,position,occurtence,match_parameter)函數

技術分享圖片

描述:字符串替換函數。相當於增強的replace函數。

Source_string指定源字符表達式;

pattern指定規則表達式;

replace_string指定用於替換的字符串;

position指定起始搜索位置;

occurtence指定替換出現的第n個字符串;

match_parameter指定默認匹配操作的文本串

Examples

--The following example examines phone_number,
-- looking for the pattern xxx.xxx.xxxx. Oracle reformats this pattern with (xxx) xxx-xxxx.
SELECT
  REGEXP_REPLACE(phone_number,
                 ([[:digit:]]{3})\.([[:digit:]]{3})\.([[:digit:]]{4}),
                 (\1) \2-\3) "REGEXP_REPLACE"
  FROM employees
  ORDER BY "REGEXP_REPLACE";

REGEXP_REPLACE
--------------------------------------------------------------------------------
(515) 123-4444
(515) 123-4567
(515) 123-4568
(515) 123-4569
(515) 123-5555

--The following example examines the string, looking for two or more spaces. 
--Oracle replaces each occurrence of two or more spaces with a single space.
SELECT REGEXP_REPLACE(500 Oracle Parkway, Redwood Shores, CA, ( ){2,}, ) "REGEXP_REPLACE" FROM DUAL; REGEXP_REPLACE -------------------------------------- 500 Oracle Parkway, Redwood Shores, CA

REGEXP_SUBSTR(source_string, pattern[,position [, occurrence[, match_parameter]]])函數

技術分享圖片

描述:返回匹配模式的子字符串。相當於增強的substr函數。

Source_string指定源字符表達式;

pattern指定規則表達式;

position指定起始搜索位置;

occurtence指定替換出現的第n個字符串;

match_parameter指定默認匹配操作的文本串。

Examples

--The following example examines the string, looking for the first substring bounded by commas. 
--Oracle Database searches for a comma followed by one or more occurrences of non-comma characters followed by a comma. 
--Oracle returns the substring, including the leading and trailing commas.
SELECT
  REGEXP_SUBSTR(500 Oracle Parkway, Redwood Shores, CA,
                ,[^,]+,) "REGEXPR_SUBSTR"
  FROM DUAL;

REGEXPR_SUBSTR
-----------------
, Redwood Shores,

--The following example examines the string, 
--looking for http:// followed by a substring of one or more alphanumeric characters and optionally,
--a period (.). Oracle searches for a minimum of three and a maximum of four occurrences of this substring between
--http:// and either a slash (/) or the end of the string.
SELECT REGEXP_SUBSTR(http://www.example.com/products, http://([[:alnum:]]+\.?){3,4}/?) "REGEXP_SUBSTR" FROM DUAL; REGEXP_SUBSTR ---------------------- http://www.example.com/ --The next two examples use the subexpr argument to return a specific subexpression of pattern.
--The first statement returns the first subexpression in pattern:
SELECT REGEXP_SUBSTR(1234567890, (123)(4(56)(78)), 1, 1, i, 1) "REGEXP_SUBSTR" FROM DUAL; REGEXP_SUBSTR ------------------- 123 --The next statement returns the fourth subexpression in pattern: SELECT REGEXP_SUBSTR(1234567890, (123)(4(56)(78)), 1, 1, i, 4) "REGEXP_SUBSTR" FROM DUAL; REGEXP_SUBSTR ------------------- 78

REGEXP_INSTR(source_string, pattern[, start_position[, occurrence[, return_option[, match_parameter]]]])函數

描述: 該函數查找 pattern ,並返回該模式的第一個位置。

您可以隨意指定您想要開始搜索的 start_position。 occurrence 參數默認為1,

除非您指定您要查找接下來出現的一個模式。return_option 的默認值為 0,它返回該模式的起始位置;

值為 1 則返回符合匹配條件的下一個字符的起始位置

技術分享圖片

Examples

--The following example examines the string, looking for occurrences of one or more non-blank characters. 
--Oracle begins searching at the first character in the string and
--returns the starting position (default) of the sixth occurrence of one or more non-blank characters.
SELECT REGEXP_INSTR(500 Oracle Parkway, Redwood Shores, CA, [^ ]+, 1, 6) "REGEXP_INSTR" FROM DUAL; REGEXP_INSTR ------------ 37 --The following example examines the string, looking for occurrences of words beginning with s, r, or p, regardless of case,
--followed by any six alphabetic characters. Oracle begins searching at the third character in the string and
--returns the position in the string of the character following the second occurrence of a seven-letter word beginning with s, r, or p,
--regardless of case.
SELECT REGEXP_INSTR(500 Oracle Parkway, Redwood Shores, CA, [s|r|p][[:alpha:]]{6}, 3, 2, 1, i) "REGEXP_INSTR" FROM DUAL; REGEXP_INSTR ------------ 28 --The following examples use the subexpr argument to search for a particular subexpression in pattern.
--The first statement returns the position in the source string of the first character in the first subexpression, which is ‘123‘:
SELECT REGEXP_INSTR(1234567890, (123)(4(56)(78)), 1, 1, 0, i, 1) "REGEXP_INSTR" FROM DUAL; REGEXP_INSTR ------------------- 1 --The next statement returns the position in the source string of the first character in the second subexpression, which is ‘45678‘: SELECT REGEXP_INSTR(1234567890, (123)(4(56)(78)), 1, 1, 0, i, 2) "REGEXP_INSTR" FROM DUAL; REGEXP_INSTR ------------------- 4 --The next statement returns the position in the source string of the first character in the fourth subexpression, which is ‘78‘: SELECT REGEXP_INSTR(1234567890, (123)(4(56)(78)), 1, 1, 0, i, 4) "REGEXP_INSTR" FROM DUAL; REGEXP_INSTR ------------------- 7

oracle正則表達式