关于甲骨文 Regexp_Replace

Regarding Oracle Regexp_Replace

我有一个类似 "Vinoth^Vinoth Karthick Vinoth^Vinoth^Vinoth" 的字符串,由“^”分隔。我只想用 XXX 替换 Vinoth。

I/P String : Vinoth^Vinoth Karthick Vinoth^Vinoth^Vinoth
Expected output : XXX^Vinoth Karthick Vinoth^XXX^XXX

请建议如何使用 Regexp_replace 或 ORACLE SQL 语句中的任何其他函数来执行此操作。

将分隔符 ^ 字符加倍并将字符串包装在分隔符 ^ 字符中,以便每个元素都有自己独特的前导和尾随分隔符,然后您只需将 ^Vinoth^ 替换为^XXX^ 并反转定界符和 trim 前导和尾随定界符的加倍:

SQL Fiddle

Oracle 11g R2 架构设置:

SELECT 1 FROM DUAL;

查询 1:

SELECT TRIM(
         '^' FROM
         REPLACE(
           REPLACE(
             '^' ||
             REPLACE(
               'Vinoth^Vinoth Karthick Vinoth^Vinoth^Vinoth',
               '^',
               '^^'
             )
             || '^',
             '^Vinoth^',
             '^XXX^'
           ),
           '^^',
           '^'
         )
       ) AS replaced
FROM   DUAL

Results:

|                           REPLACED |
|------------------------------------|
| XXX^Vinoth Karthick Vinoth^XXX^XXX |

还有一个选择:

SQL> with test (col) as
  2    (select 'Vinoth^Vinoth Karthick Vinoth^Vinoth^Vinoth' from dual),
  3  inter as
  4    (select regexp_substr(replace(col, '^', ':'), '[^:]+', 1, level) col,
  5            level lvl
  6     from test
  7     connect by level <= regexp_count(col, '\^') + 1
  8    )
  9  select listagg(regexp_replace(col, '^Vinoth$', 'XXX'), '^')
 10    within group (order by lvl) result
 11  from inter;

RESULT
-----------------------------------------------------------------------------

XXX^Vinoth Karthick Vinoth^XXX^XXX

SQL>