首页 > 解决方案 > 使用正则表达式在列中查找和追加

问题描述

我有一张名为 test 的表。我需要使用正则表达式查找任何字母并在名称列中附加 ALL。请需要您的意见。

注意:将来的值也将插入该列。所以在运行时它应该替换。

create table test(id number, name varchar2(10));
insert INTO TEST VALUES (1,'_A');
insert INTO TEST VALUES (2,'_F');
insert INTO TEST VALUES (3,'_K');
insert INTO TEST VALUES (4,'_B || _G');

我的输出应该如下所示

-------------------------------
  id    column
--------------------------------
    1   _AALL
    2   _FALL
    3   _KALL
    4   _BALL || _GALL

标签: sqloracle

解决方案


您可以使用regexp_replace

with test as(
  select 1 as id , '_A' as l from dual union all
  select 1 as id , '_B || _G' as l from dual 
)

select t.*
      , trim(regexp_replace(t.l,'(_[A-Z])( |$)','\1'||'ALL '))
  from test t

推荐阅读