【问题标题】:REGEXP_REPLACE expression issueREGEXP_REPLACE 表达式问题
【发布时间】:2020-02-21 15:30:18
【问题描述】:

我在下面得到了一些 sql 脚本来删除单词之间的单词,但它会找到我的 (/hide) 的最后一次出现而不是第一次出现。需要帮助才能按预期获得输出。谢谢。

select regexp_replace('(hide)it(/hide)should be show(hide)my(/hide) text',
       '^\(hide\).*\(/hide\)', '') "TESTING" 
  from dual;

我希望输出是:

should be show text

但实际输出是:

text

如果我的数据位于数据类型为 clob 的列之一中。目前我使用下面的脚本来选择。例如,我的表是 testing_table,其列 desc_str 和 data_type 为 clob,其中包含值 '(hide)it(/hide)should be show(hide)my(/hide) text';

select trim(to_char(regexp_replace(desc_str,'^\(hide\).*\(/hide\)',''))) as desc 
  from testing_table 
 where OOE_FP_SS_ID = $id;

【问题讨论】:

  • 我使用的是oracle sql developer。

标签: sql regex oracle regexp-replace


【解决方案1】:

根据您的示例:

SQL> with test (col) as
  2    (select '(hide)it(/hide)should be show(hide)my(/hide) text' from dual)
  3  select regexp_replace(col, '\(hide\)\w+\(/hide\)', '') result
  4  from test;

RESULT
-------------------
should be show text

SQL>

[编辑:CLOB]

您要求 CLOB。这是一个例子:

SQL> create table test (desc_str clob);

Table created.

SQL> insert into test values ('(hide)it(/hide)should be show(hide)my(/hide) text');

1 row created.

SQL> select regexp_replace(desc_str, '\(hide\)\w+\(/hide\)', '') result from test;

RESULT
--------------------------------------------------------------------------------
should be show text

SQL>

另一个例子:

SQL> with test (desc_str) as
  2    (select '(hide)ItemId: 4334(/hide)|(hide)Description Item - SubType:(/hide) Name - Item|(irow)Price Value: MYR 1,668' from dual)
  3  select regexp_replace(desc_str, '\(hide\).+?\(/hide\)', '') result from test;
                                                ^
RESULT                                          | this question mark was missing
------------------------------------------
| Name - Item|(irow)Price Value: MYR 1,668

SQL>

【讨论】:

  • 如果我的文本 '(hide)it(/hide)should be show(hide)my(/hide) text' 是表中数据类型为 clob 的列之一呢?跨度>
  • 试试看会发生什么。
  • with test (col) as (select to_char(desc_str) from testing_table where id = 6041) select regexp_replace(col, '(hide)\w+(/hide)', '') result from test ;
  • 我尝试使用上面的代码它无法隐藏隐藏之间的单词
  • 我尝试上面的数据工作正常,但我可以知道为什么下面的数据无法隐藏。 (hide)ItemId: 4334(/hide)|(hide)Description Item - SubType:(/hide) Name - Item|(irow)价格: MYR 1,668
猜你喜欢
  • 2018-08-17
  • 1970-01-01
  • 1970-01-01
  • 2019-06-28
  • 2010-09-27
  • 2011-04-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多