小编典典

使用Oracle SQL提取某些模式

sql

我有一个像

with xx as (
select 'id9' idno,'untest X456789,W357987 and Q321089 cont group' test from dual)
select * from xx

有一些类似下面的thosand行

       IDNO |                             TEST
      +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
        id9 | untest X456789,W357987 and Q321089 cont group

我想提取以字母后跟6位数字开头的单词。 另外,它们之间应该有一个逗号(因为以后我会将它们放在多行中)

结果表:

        IDNO |                TEST
      +++++++++++++++++++++++++++++++++++++++++
        id9 | X456789,X321678,W357987,Q321089

我已经尝试过regexp_replace,但无法解决。

select idno, REGEXP_replace( test,'([^[A-Z]{1}[:digit:]{6},?])') AS test from xx

阅读 170

收藏
2021-04-22

共1个答案

小编典典

以下是您想要的原始字符串的用途:

with xx as (
select 'id9' idno,'untest X456789,W357987 and Q321089 cont group' test from dual
)
select idno,
       REGEXP_replace(test,
                      '([A-Z]{1}[0-9]{6}[ ,]?)|(.)', '\1'
                     ) AS test
from xx;

使第二个空格成为逗号。。。您可以使用常规替换:

with xx as (
select 'id9' idno,'untest X456789,W357987 and Q321089 cont group' test from dual
)
select idno,
       replace(REGEXP_replace(test,
                              '([A-Z]{1}[0-9]{6}[ ,]?)|(.)', '\1'
                             ),
               ' ', ',') AS test
from xx;

SQL小提琴在这里

2021-04-22