Oracle 中使用正則表達式


在 Oracle 中使用正則表達式

Oracle使用正則表達式離不開這4個函數:

1。regexp_like

2。regexp_substr

3。regexp_instr

4。regexp_replace

看函數名稱大概就能猜到有什么用了。

 

regexp_like 只能用於條件表達式,和 like 類似,但是使用的正則表達式進行匹配,語法很簡單:

regexp_like_condition

regexp_substr 函數,和 substr 類似,用於拾取合符正則表達式描述的字符子串,語法如下:

regexp_substrregexp_instr 函數,和 instr 類似,用於標定符合正則表達式的字符子串的開始位置,語法如下:

regexp_instr

regexp_replace 函數,和 replace 類似,用於替換符合正則表達式的字符串,語法如下:

regexp_replace

這里解析一下幾個參數的含義:

1。source_char,輸入的字符串,可以是列名或者字符串常量、變量。搜索字符串。可以是任意的數據類型char,VARCHAR2,nchar,CLOB,NCLOB類型

2。pattern,正則表達式。

3。match_parameter,匹配選項。

        取值范圍: i:大小寫不敏感; c:大小寫敏感;n:點號 . 不匹配換行符號;m:多行模式;x:擴展模式,忽略正則表達式中的空白字符。

4。position,標識從第幾個字符開始正則表達式匹配。可選。搜索在字符串中的開始位置。如果省略,默認為1,這是第一個位置的字符串。

5。occurrence,標識第幾個匹配組。可選。它是模式字符串中的第n個匹配位置。如果省略,默認為1。

6。replace_string,替換的字符串。

  • regexp_like

    function regexp_like(str,pattern)

     str :需要正則處理的字符串
     pattern : 進行匹配的正則表達式
  • 模糊查詢和 like 類似

  • REGEXP_SUBSTR

    function REGEXP_SUBSTR(str, pattern, position, occurrence, modifier)

     str     :需要進行正則處理的字符串
     
     pattern    :進行匹配的正則表達式
     
     position   :起始位置,從第幾個字符開始正則表達式匹配(默認為1)
     
     occurrence :標識第幾個匹配組,默認為1
     
     modifier   :模式('i'不區分大小寫進行檢索;'c'區分大小寫進行檢索。默認為'c'。)
  • 截取匹配的 pattern 的字符串,多個匹配截取第 occurrence 個匹配組

  • REGEXP_INSTR

  • REGEXP_REPLACE

     function regexp_replace(str,pattern,replaceStr, position, occurrence, modifier)
    
     6個參數
     
     第一個是輸入的字符串
     
     第二個是正則表達式
     
     第三個是替換的字符
     
     第四個是標識從第幾個字符開始正則表達式匹配。(默認為1)
     
     第五個是標識第幾個匹配組。(默認為全部都替換掉)
     
     第六個是是取值范圍:
     
     i:大小寫不敏感;
     
     c:大小寫敏感;
     
     n:點號 . 不匹配換行符號;
     
     m:多行模式;
     
     x:擴展模式,忽略正則表達式中的空白字符。
     
     (1) 替換匹配的字符串
     		select regexp_replace('abc123wxz','[a-zA-Z]+','') str  from dual
     	    結果 : 123
     	    把字母替換成空
     		 
     (2)也可以替換連續匹配多個子表達式位置
     		select regexp_replace('123abc','(\d+)(\w+)','\2\1') from dual
     		結果 : abc123

 

描述
^ 匹配一個字符串的開始。如果與“m” 的match_parameter一起使用,則匹配表達式中任何位置的行的開頭。
$ 匹配字符串的結尾。如果與“m” 的match_parameter一起使用,則匹配表達式中任何位置的行的末尾。
* 匹配零個或多個。
+ 匹配一個或多個出現。
匹配零次或一次出現。
匹配任何字符,除了空。
| 用“OR”來指定多個選項。
[] 用於指定一個匹配列表,您嘗試匹配列表中的任何一個字符。
[^] 用於指定一個不匹配的列表,您嘗試匹配除列表中的字符以外的任何字符。
() 用於將表達式分組為一個子表達式。
{M} 匹配m次。
{M,} 至少匹配m次。
{M,N} 至少匹配m次,但不多於n次。
\ n n是1到9之間的數字。在遇到\ n之前匹配在()內找到的第n個子表達式。
[..] 匹配一個可以多於一個字符的整理元素。
[:] 匹配字符類。
[==] 匹配等價類。
\ d 匹配一個數字字符。
\ D 匹配一個非數字字符。
\ w 匹配包括下划線的任何單詞字符。
\ W 匹配任何非單詞字符。
\ s 匹配任何空白字符,包括空格,制表符,換頁符等等。
\ S 匹配任何非空白字符。
\A 在換行符之前匹配字符串的開頭或匹配字符串的末尾。
\Z 匹配字符串的末尾。
*? 匹配前面的模式零次或多次發生。
+? 匹配前面的模式一個或多個事件。
?? 匹配前面的模式零次或一次出現。
{N}? 匹配前面的模式n次。
{N,}? 匹配前面的模式至少n次。
{N,M}? 匹配前面的模式至少n次,但不超過m次。

說了一堆文縐縐的,現在開始實例演練了,在此之前先建好一個表。

復制代碼
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+$');
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 -- 將第一、第二捕獲組交換位置,用尖括號標識出來
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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM