如何使用正则表达式跳过String的一部分?

时间:2018-02-28 10:43:29

标签: sql regex oracle

我有这个字符串

12345678(a part of string); 12345679(a part of string); ABCD220785; ABCD220785

我想要像这样的输出

12345678(a part of string); 12345679(a part of string); ABCD220785

我创建了以下函数来删除重复项,但它也删除了字符串

CREATE OR REPLACE FUNCTION SplitString(V IN VARCHAR2)
  RETURN VARCHAR2
IS
  RETVAL VARCHAR2(4000);
BEGIN
WITH cte (t,dt) AS
  ( 
   SELECT DISTINCT 1 AS t,str AS dt
   FROM
    (SELECT regexp_substr (V, '[^; ]+',1, rownum) str
    FROM dual
      CONNECT BY level <= regexp_count (V, '[^; ]+')
    )
  )
SELECT DISTINCT listagg(dt,';') within GROUP (
ORDER BY t) AS servicename
INTO RETVAL
FROM cte
GROUP BY t;
RETURN RETVAL;
END;

1 个答案:

答案 0 :(得分:0)

这是一个选项:

SQL> with test (id, col) as
  2    (select 1, '12345678(a part of string); 12345679(a part of string); ABCD220785;ABCD220785'
  3     from dual
  4     union
  5     select 2, 'ABCD220785;ABCD220785'
  6     from dual
  7    ),
  8  t_rows as
  9    (select id, trim(regexp_substr(col, '[^;]+', 1, column_value)) col, column_value rn
 10     from test,
 11          table(cast(multiset(select level from dual
 12                              connect by level <= regexp_count(col, ';') + 1
 13                             ) as sys.odcinumberlist))
 14    ),
 15  t_distinct as
 16    (select id, col, min(rn) min_rn
 17     from t_rows
 18     group by id, col
 19    )
 20  select id, listagg(col, '; ') within group (order by min_rn) result
 21  from t_distinct
 22  group by id;

        ID RESULT
---------- ----------------------------------------------------------------------
         1 12345678(a part of string); 12345679(a part of string); ABCD220785
         2 ABCD220785

SQL>