1

我有一个字符串

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

有一些像下面这样的行

       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
4

1 回答 1

4

以下为您的原始字符串执行您想要的操作:

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 Fiddle 在这里

于 2013-08-02T12:37:44.563 回答