【问题标题】:How to remove one or more instances of '?*' in a string using REGEXP_REPLACE function in oracle?如何使用 oracle 中的 REGEXP_REPLACE 函数删除字符串中的一个或多个“?*”实例?
【发布时间】:2019-11-20 21:43:50
【问题描述】:

我在名为 Line 的列中将字符 '?*' 重复一到三遍。我需要删除这些字符。我如何使用ReplaceREGEXP_REPLACE 来做到这一点?

SELECT
    Line, REGEXP_REPLACE(LINE,'[?*]','')--([^\+]+)
FROM
    TABLE
WHERE
    INSTR(LINE,'?*') != 0;

在哪里

REGEXP_REPLACE(LINE,'\?*','') 单独替换了?,而* 保持不变。

REGEXP_REPLACE(LINE,'?*','') 替换 nothing

REGEXP_REPLACE(LINE,'[?*]','') 替换所有?s 和所有*s。我只在?* 组合为?* 时替换。

【问题讨论】:

  • 样本数据(和所需的输出)可能会对我们有所帮助。

标签: sql regex oracle regexp-replace


【解决方案1】:

如果需要删除字符串'?*',可以使用replace

SQL> with test(string) as (
  2      select 'aa?*b?'            from dual union all
  3      select 'a*a?*??b?'         from dual union all
  4      select 'a*a??b???c*?**cc'  from dual union all
  5      select 'aa?b?*?cc?d??*?*?' from dual
  6  )
  7  select string, replace(string, '?*', '') as result
  8  from test;

STRING            RESULT
----------------- ---------------
aa?*b?            aab?
a*a?*??b?         a*a??b?
a*a??b???c*?**cc  a*a??b???c**cc
aa?b?*?cc?d??*?*? aa?b?cc?d??

【讨论】:

  • 这很有效,谢谢。我假设替换只会在第一次出现而不是在后续出现时起作用。
  • @Arun replace 文档
【解决方案2】:

使用(\?\*) 作为模式:

with tab(line) as
( 
 select 'abc?*ghh*?g?l*'  from dual union all
 select '?*U?KJ*H'        from dual union all
 select '*R5?4*&t?*frg?*' from dual
)
select regexp_replace(line,'(\?\*)','') as "Result String"
  from tab;

Result String
-------------
abcghh*?g?l*
U?KJ*H
*R5?4*&tfrg

Demo

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-01-06
    • 2021-03-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-08
    • 2014-09-15
    • 2015-04-22
    相关资源
    最近更新 更多