oracle中的支持正則表達式的函數主要有下面四個:
git
1,REGEXP_LIKE :與LIKE的功能類似正則表達式
2,REGEXP_INSTR :與INSTR的功能類似sql
3,REGEXP_SUBSTR :與SUBSTR的功能類似oracle
4,REGEXP_REPLACE :與REPLACE的功能類似ide
regexp_like 只能用於條件表達式,和 like 相似,可是使用的正則表達式進行匹配,語法很簡單:函數
regexp_substr 函數,和 substr 相似,用於拾取合符正則表達式描述的字符子串,語法以下:spa
上面這個模式參數就複雜了些 經常使用組合如下.net
regexp_substr (string, pattern, position)regexp
regexp_substr (string, pattern, position, occurence)orm
regexp_substr (string, pattern, position, occurence, match_parameters)
regexp_instr 函數,和 instr 相似,用於標定符合正則表達式的字符子串的開始位置,語法以下:
regexp_instr (string, pattern)
regexp_instr (string, pattern, position)
regexp_instr (string, pattern, position, occurence)
regexp_instr (string, pattern, position, occurence, return-option)
regexp_instr (string, pattern, position, occurence, return-option, parameters)
regexp_replace 函數,和 replace 相似,用於替換符合正則表達式的字符串,語法以下:
這裏解析一下幾個參數的含義:
1。source_char,輸入的字符串,能夠是列名或者字符串常量、變量。
2。pattern,正則表達式。
3。match_parameter,匹配選項取值範圍: i:大小寫不敏感; c:大小寫敏感;n:點號 . 不匹配換行符號;m:多行模式;x:擴展模式,忽略正則表達式中的空白字符。
4。position,標識從第幾個字符開始正則表達式匹配。
5。occurrence,標識第幾個匹配組。
6。replace_string,替換的字符串。
在新的函數中使用正則表達式來代替通配符‘%’和‘_’。
正則表達式由標準的元字符(metacharacters)所構成:
'^' 匹配輸入字符串的開始位置,在方括號表達式中使用,此時它表示不接受該字符集合。
'$' 匹配輸入字符串的結尾位置。若是設置了 RegExp 對象的 Multiline 屬性,則 $ 也匹配 'n' 或 'r'。
'.' 匹配除換行符 n以外的任何單字符。
'?' 匹配前面的子表達式零次或一次。
'+' 匹配前面的子表達式一次或屢次。
'*' 匹配前面的子表達式零次或屢次。
'|' 指明兩項之間的一個選擇。例子'^([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 位置和順序
| 「或」操做
說了一堆文縐縐的,如今開始實例演練了,在此以前先建好一個表。
create table tmp as with data as ( select 'like' as id ,'a9999' as str from dual union all select 'like' ,'a9c' from dual union all select 'like' ,'A7007' from dual union all select 'like' ,'123a34cc' from dual union all select 'substr' ,'123,234,345' from dual union all select 'substr' ,'12,34.56:78' from dual union all select 'substr' ,'123456789' from dual union all select 'instr' ,'192.168.0.1' from dual union all select 'replace' ,'(020)12345678' from dual union all select 'replace' ,'001517729C28' from dual ) select * from data ;
select * from tmp ; ID STR ------- ------------- like a9999 like a9c like A7007 like 123a34cc substr 123,234,345 substr 12,34.56:78 substr 123456789 instr 192.168.0.1 replace (020)12345678 replace 001517729C28
regexp_like 例子:
select str from tmp where id='like' and regexp_like(str,'A\d+','i'); -- 'i' 忽略大小寫 STR ------------- a9999 a9c A7007 123a34cc
select str from tmp where id='like' and regexp_like(str, 'a\d+'); STR ------------- a9999 a9c 123a34cc
select str from tmp where id='like' and regexp_like(str,'^a\d+'); STR ------------- a9999 a9c
select str from tmp where id='like' and regexp_like(str,'^a\d+c$'); STR ------------- a9999
regexp_substr 例子:
col str format a15; select str, regexp_substr(str,'[^,]+') str, regexp_substr(str,'[^,]+',1,1) str, regexp_substr(str,'[^,]+',1,2) str, -- occurrence 第幾個匹配組 regexp_substr(str,'[^,]+',2,1) str -- position 從第幾個字符開始匹配 from tmp where id='substr'; STR STR STR STR STR --------------- --------------- --------------- --------------- --------------- 123,234,345 123 123 234 23 12,34.56:78 12 12 34.56:78 2 123456789 123456789 123456789 23456789 select str, regexp_substr(str,'\d') str, regexp_substr(str,'\d+' ,1,1) str, regexp_substr(str,'\d{2}',1,2) str, regexp_substr(str,'\d{3}',2,1) str from tmp where id='substr'; STR STR STR STR STR --------------- --------------- --------------- --------------- --------------- 123,234,345 1 123 23 234 12,34.56:78 1 12 34 123456789 1 123456789 34 234 select regexp_substr('123456789','\d',1,level) str --取出每位數字,有時這也是行轉列的方式 from dual connect by level<=9 STR --------------- 1 2 3 4 5 6 7 8 9
regex_instr 例子:
col ind format 9999; select str, regexp_instr(str,'\.' ) ind , regexp_instr(str,'\.',1,2) ind , regexp_instr(str,'\.',5,2) ind from tmp where id='instr'; STR IND IND IND --------------- ----- ----- ----- 192.168.0.1 4 8 10 select regexp_instr('192.168.0.1','\.',1,level) ind , -- 點號. 所在的位置 regexp_instr('192.168.0.1','\d',1,level) ind -- 每一個數字的位置 from dual connect by level <= 9 IND IND ----- ----- 4 1 8 2 10 3 0 5 0 6 0 7 0 9 0 11 0 0
regex_replace 例子:
select str, regexp_replace(str,'020','GZ') str, regexp_replace(str,'(\d{3})(\d{3})','<\2\1>') str -- 將第1、第二捕獲組交換位置,用尖括號標識出來 from tmp where id='replace'; STR STR STR --------------- --------------- --------------- (020)12345678 (GZ)12345678 (020)<456123>78 001517729C28 001517729C28 <517001>729C28
綜合應用的例子:
col row_line format a30; with sudoku as ( select '020000080568179234090000010030040050040205090070080040050000060289634175010000020' as line from dual ), tmp as ( select regexp_substr(line,'\d{9}',1,level) row_line, level col from sudoku connect by level<=9 ) select regexp_replace( row_line ,'(\d)(\d)(\d)(\d)(\d)(\d)(\d)(\d)(\d)','\1 \2 \3 \4 \5 \6 \7 \8 \9') row_line from tmp ROW_LINE ------------------------------ 0 2 0 0 0 0 0 8 0 5 6 8 1 7 9 2 3 4 0 9 0 0 0 0 0 1 0 0 3 0 0 4 0 0 5 0 0 4 0 2 0 5 0 9 0 0 7 0 0 8 0 0 4 0 0 5 0 0 0 0 0 6 0 2 8 9 6 3 4 1 7 5 0 1 0 0 0 0 0 2 0