簡體   English   中英

使用正則表達式更新 Oracle

[英]Oracle Update with Regular Expression

我在列中有一些值,例如:

“這是測試 $ABC 變量。”

我想將所有變量定義更新為以下一個:

“這是測試 $ABC$ 變量。”

我該如何寫這個更新聲明? 不知何故,我無法應付。

你只想要replace()嗎?

update t
    set col = replace(col, '$ABC', '$ABC$')
     where col like '%$ABC%';

不幸的是,我不擅長正則表達式,所以我這個糟糕的嘗試可能會被更聰明的東西所取代。 期待看到它!

SQL> with test as
  2    (select 'This is test $ABC variables' col from dual union
  3     select 'Stackoverflow says $DEF is so cool' from dual
  4    )
  5  select
  6    col,
  7    regexp_replace(col, '\$\w+',  regexp_substr(col, '\$\w+') || '$') result
  8  from test;

COL                                RESULT
---------------------------------- ----------------------------------------
Stackoverflow says $DEF is so cool Stackoverflow says $DEF$ is so cool
This is test $ABC variables        This is test $ABC$ variables

SQL>

您可以使用此REGEXP查詢。 '\\$(\\w+)'的括號捕獲\\1 $后面的單詞

SELECT REGEXP_REPLACE (
          'This is test $ABC variables.This is a second variable $variable2.I have 100$',
          '\$(\w+)',
          '$\1$')
  FROM DUAL;

O/P:

This is test $ABC$ variables.This is a second variable $variable2$.I have 100$

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM