0
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 

我必须从结果中删除“,”。我也想要最后一个字符串作为输出。即“一、二、三”中的三个和八、十九、五”中的五个我该怎么做??

4

2 回答 2

1

如果只想获取最后一个单词而不检查您的字符串是否符合特定模式:

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
于 2012-11-20T09:53:53.100 回答
0
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;
于 2012-11-20T09:46:51.603 回答