[英]REGEXP_SUBSTR desired output
SELECT REGEXP_SUBSTR('one,two,three',',[^,]+') AS reg_result FROM DUAL;
REG_RESULT
,two
SELECT REGEXP_SUBSTR('eight,nineteen,five',',[^,]+') AS reg_result FROM DUAL;
REG_RESULT
,nineteen
我必須從結果中刪除“,” 。 我也希望最后一個字符串作為輸出 。 即三位來自“一,二,三”及五名來自“八項,十九歲,五”。 我該怎么做??
如果只想獲取最后一個單詞而不檢查您的字符串是否符合特定模式:
SQL> with t1 as(
2 select 'one,two,three' as str from dual
3 )
4 select regexp_substr(str, '([[:alpha:]]+)$') last_word
5 from t1
6 ;
LAST_WORD
---------
three
對評論的回應
如何從第一個字符串中獲取字符串二和從第二個字符串中獲取十九?
regexp_substr
函數的第四個參數是模式的出現。 因此,要獲取字符串中的第二個單詞,我們可以使用regexp_substr(str, '[^,]+', 1, 2)
SQL> with t1 as(
2 select 'one,two,three' as str from dual
3 )
4 select regexp_substr(str, '[^,]+', 1, 2) as Second_Word
5 from t1;
Second_Word
---------
two
如果需要從字符串中提取每個單詞:
-- sample of data from your question
SQL> with t1 as(
2 select 'one,two,three' as str from dual union all
3 select 'eight,nineteen,five' from dual
4 ), -- occurrences of the pattern
5 occurrence as(
6 select level as ps
7 from ( select max(regexp_count(str, '[^,]+')) mx
8 from t1
9 ) s
10 connect by level <= s.mx
11 ) -- the query
12 select str
13 , regexp_substr(str, '[^,]+', 1, o.ps) word
14 , o.ps as word_num
15 from t1 t
16 cross join occurrence o
17 order by str
18 ;
STR WORD WORD_NUM
------------------- ----------- ----------
eight,nineteen,five eight 1
eight,nineteen,five nineteen 2
eight,nineteen,five five 3
one,two,three three 3
one,two,three one 1
one,two,three two 2
6 rows selected
SELECT REGEXP_SUBSTR(REGEXP_SUBSTR('one,two,three',',[^,]+$'),'[^,]+') AS reg_result FROM DUAL;
我不確定Oracle是否具有后顧之憂,但您也可以嘗試以下操作:
SELECT REGEXP_SUBSTR('one,two,three','(?<=,)[^,]+$') AS reg_result FROM DUAL;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.