這幾天學習regexp寫的小例子

記錄一下,總感覺會用到,終於可以關電腦了

1.oracle學習中,經常會用到dual這張表,那麼你試過select * from dual;???
語句

SELECT * FROM dual;

結果:

DUMMY
X

dummy:虛擬的,假的。
2. 要點:僞列 level,lpad 填充函數,connect by 子句

lpad( string, padded_length, [ pad_string ] )

lpad函數是Oracle數據庫函數,lpad函數從左邊對字符串使用指定的字符進行填充。從其字面意思也可以理解,l是left的簡寫,pad是填充的意思,所以lpad就是從左邊填充的意思。

with x as
( select 'aa' chr from dual
union all
select 'bb' chr from dual)
select level ,chr,lpad( ' ' ,( LEVEL)* 5 , '-' )||chr other from x connect by level <= 2;

結果:

1   aa  ---- aa
2   aa  --------- aa
2   bb  --------- bb
1   bb  ---- bb
2   aa  --------- aa
2   bb  --------- bb

3.要點:connect by 子句,僞列 level,rownum
方法一

SELECT '愛我中國'||LEVEL||'-123456' FROM dual CONNECT BY LEVEL <=12;

結果:

愛我中國1-123456
愛我中國2-123456
愛我中國3-123456
愛我中國4-123456
愛我中國5-123456
愛我中國6-123456
愛我中國7-123456
愛我中國8-123456
愛我中國9-123456
愛我中國10-123456
愛我中國11-123456
愛我中國12-123456

方法二

SELECT '愛我中國'||ROWNUM||'-123456' FROM dual CONNECT BY ROWNUM <=12;

結果:

愛我中國1-123456
愛我中國2-123456
愛我中國3-123456
愛我中國4-123456
愛我中國5-123456
愛我中國6-123456
愛我中國7-123456
愛我中國8-123456
愛我中國9-123456
愛我中國10-123456
愛我中國11-123456
愛我中國12-123456

4.regexp用法練習
regexp用法學習

-- regexp_replace 
SELECT * FROM tmp;
SELECT t.STR,
       REGEXP_replace(t.STR,'020','gz'),
             REGEXP_replace(t.STR,'(\d{3})(\d{3})','<\2\1>',3)
FROM tmp t
WHERE t.ID = 'replace';

-- regexp_like 只能用於條件表達式
-- 查詢id爲like,同時字符串中包含以a|A開頭,後面爲數字的字符串
SELECT str FROM tmp t WHERE t.ID = 'like' AND regexp_like(t.STR,'a\d+','i');
-- 查詢id爲like,同時字符串開頭爲a|A,後面爲數字的字符串
SELECT str FROM tmp t WHERE t.ID = 'like' AND regexp_like(t.STR,'^a\d+','i');
-- 
SELECT str FROM tmp t WHERE t.ID = 'like' AND regexp_like(t.STR,'^a\d+$','i');

-- ERGEXP_substr 字符串的截取
-- regexp_substr(source_char,pattern,position,occurrence)
-- regexp_substr(輸入的字符串,正則表達式,標識從第幾個字符開始匹配正則表達式,標識第幾個匹配項)
SELECT t.STR,
       REGEXP_substr(t.STR,'[^,]+') str,
             regexp_substr(t.STR,'[^,]+',1,1) str,
             regexp_substr(t.STR,'[^,]+',1,2) str,
             regexp_substr(t.STR,'[^,]+',2,1) str,
             regexp_substr(t.STR,'[^,]+',2,2) str
FROM tmp t WHERE t.id =  'substr';             o

SELECT regexp_substr('123456789','\d',1,LEVEL) FROM dual CONNECT BY LEVEL <=9; 

-- regexp_instr 
-- regexp_instr(source_char,pattern,position,occurrence,return_option,match_parameter)
-- regexp_instr(輸入的字符串,正則表達式,標識從第幾個 字符 開始匹配正則表達式,標識第幾個匹配項,返回項,匹配選項)

SELECT t.STR,
       regexp_instr(t.STR,'\.') ind,
             regexp_instr(t.STR,'\.',1,1) ind,
             REGEXP_instr(t.STR,'\.',1,2) ind,
             regexp_instr(t.STR,'\.',2,2) ind,
             regexp_instr(t.STR,'\.',5,2) ind
  FROM tmp t WHERE t.ID = 'instr'; 

------------------
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
-----------------
SELECT * FROM tmp t WHERE t.ID = 'instr';   
-----------------
SELECT /*t.STR,
       regexp_instr(t.STR,'\.',1,LEVEL) ind,
             regexp_instr(t.STR,'\d',1,LEVEL) ind,*/
             regexp_instr('192.168.0.1','\.',1,LEVEL) ind,
             regexp_instr('192.168.0.1','\d',1,LEVEL) ind,
             regexp_instr('192.168.0.1','\.',1,level) ind ,  -- 點號. 所在的位置
       regexp_instr('192.168.0.1','\d',1,level) ind,   -- 每個數字的位置
             LEVEL ROWSnum
FROM dual connect by level <=  9 ;  
--------------------
--一個有趣的玩具
--------------------

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;
-----------------
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 (
  select regexp_substr(line,'\d{9}',1,level) row_line,
  level col
  from (
  select '020000080568179234090000010030040050040205090070080040050000060289634175010000020' as line
  from dual)
  connect by level<=9
)
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章