就像我有一個字串“123456,852369,7852159,1596357”輸出尋找“1234,8523,7852,1596”
要求是....我們想在每個“,”分隔符之后收集 4 個字符
像拆分、子串和再次連接
select
REGEXP_REPLACE('MEDA,MEDA,MEDA,MEDA,MEDA,MEDA,MEDA,MEDA,MDCB,MDCB,MDCB,MDCB,MDCB,MDCB', '([^,] )(,\1) ', '\1')
from dual;
uj5u.com熱心網友回復:
我們想在每個 ',' 分隔符后收集 4 個字符
這是一種使用方法regexp_replace:
select regexp_replace(
'123456,852369,7852159,1596357',
'([^,]{4})[^,]*(,|$)',
'\1\2'
)
from dual
正則運算式分解:
([^,]{4}) 4 characters others than "," (capture that group as \1)
[^,]* 0 to n characters other than "," (no capture)
(,|$) either character "," or the end of string (capture this as \2)
該函式將每個匹配項替換為捕獲 1(我們想要的 4 個字符),然后是捕獲 2(分隔符,如果有的話)。
演示:
| 結果 |
|---|
| 1234,8523,7852,1596 |
uj5u.com熱心網友回復:
一種選擇可能是拆分字串,提取 4 個字符并將它們聚合回來:
SQL> with test (col) as
2 (select '123456,852369,7852159,1596357' from dual)
3 select listagg(regexp_substr(col, '[^,]{4}', 1, level), ',')
4 within group (order by level) result
5 from test
6 connect by level <= regexp_count(col, ',') 1;
RESULT
--------------------------------------------------------------------------------
1234,8523,7852,1596
SQL>
uj5u.com熱心網友回復:
與REGEX_REPLACE:
select regexp_replace(the_string, '(^|,)([^,]{4})[^,]*', '\1\2')
from mytable;
這尋找
- 字串或逗號的開頭
- 然后是四個不是逗號的字符
- 然后是任意數量的不是逗號的尾隨字符
并且只保留
- 開頭或逗號
- 接下來的四個字符
演示:https ://dbfiddle.uk/efUFvKyO
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/535247.html
標籤:数据库细绳甲骨文分裂级联
