简体   繁体   English

使用正则表达式更新 Oracle

[英]Oracle Update with Regular Expression

I have some values in a column like:我在列中有一些值,例如:

"This is test $ABC variables." “这是测试 $ABC 变量。”

I want to update all the variable definition to below one:我想将所有变量定义更新为以下一个:

"This is test $ABC$ variables." “这是测试 $ABC$ 变量。”

How can I write this update statement?我该如何写这个更新声明? Somehow, I couldn't manage.不知何故,我无法应付。

Do you just want replace() ?你只想要replace()吗?

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

Unfortunately, I'm not good at regular expressions so this poor attempt of mine could probably be replaced by something smarter.不幸的是,我不擅长正则表达式,所以我这个糟糕的尝试可能会被更聪明的东西所取代。 Looking forward to see it!期待看到它!

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>

You may use this REGEXP query.您可以使用此REGEXP查询。 The parentheses in '\\$(\\w+)' capture the word following $ in \\1 '\\$(\\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: 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