如何在regexp_replace函数中使用case语句?

问题描述 投票:0回答:1

我尝试在regexp_replace函数中使用case语句,因为我想根据不同的情况替换它。 SQL 看起来像这样:

col str for a15
with temp as
(
select 'A*((B*C)+D)' expre1 from dual union all
select 'A*((B*C)-D)' expre1 from dual union all
select '((A*B)+C)*D' expre1 from dual union all
select '((A*B)+C)+D' expre1 from dual union all
select 'A*((B+C)+D)' expre1 from dual union all
select '((A*B)+C)-D' expre1 from dual union all
select 'A*((B+C)-D)' expre1 from dual union all
select '((A*B)+C)/D' expre1 from dual union all
select '((A*B)-C)*D' expre1 from dual union all
select '((A*B)-C)+D' expre1 from dual union all
select 'A*((B-C)+D)' expre1 from dual union all
select 'A*((B-C)-D)' expre1 from dual union all
select '((A*B)-C)-D' expre1 from dual union all
select '((A*B)-C)/D' expre1 from dual union all
select 'A*((B/C)+D)' expre1 from dual union all
select 'A*((B/C)-D)' expre1 from dual union all
select 'A+((B*C)+D)' expre1 from dual union all
select 'A+((B*C)-D)' expre1 from dual union all
select '((A+B)+C)*D' expre1 from dual union all
select 'A+((B+C)+D)' expre1 from dual union all
select '((A+B)+C)+D' expre1 from dual union all
select 'A+((B+C)-D)' expre1 from dual union all
select '((A+B)+C)-D' expre1 from dual union all
select '((A+B)+C)/D' expre1 from dual union all
select '((A+B)-C)*D' expre1 from dual union all
select 'A+((B-C)+D)' expre1 from dual union all
select '((A+B)-C)+D' expre1 from dual union all
select '((A+B)-C)-D' expre1 from dual union all
select 'A+((B-C)-D)' expre1 from dual union all
select '((A+B)-C)/D' expre1 from dual union all
select 'A+((B/C)+D)' expre1 from dual union all
select 'A+((B/C)-D)' expre1 from dual union all
select 'A-((B*C)+D)' expre1 from dual union all
select 'A-((B*C)-D)' expre1 from dual union all
select '((A-B)+C)*D' expre1 from dual union all
select 'A-((B+C)+D)' expre1 from dual union all
select '((A-B)+C)+D' expre1 from dual union all
select 'A-((B+C)-D)' expre1 from dual union all
select '((A-B)+C)-D' expre1 from dual union all
select '((A-B)+C)/D' expre1 from dual union all
select '((A-B)-C)*D' expre1 from dual union all
select 'A-((B-C)+D)' expre1 from dual union all
select '((A-B)-C)+D' expre1 from dual union all
select 'A-((B-C)-D)' expre1 from dual union all
select '((A-B)-C)-D' expre1 from dual union all
select '((A-B)-C)/D' expre1 from dual union all
select 'A-((B/C)+D)' expre1 from dual union all
select 'A-((B/C)-D)' expre1 from dual union all
select 'A/((B*C)+D)' expre1 from dual union all
select 'A/((B*C)-D)' expre1 from dual union all
select '((A/B)+C)*D' expre1 from dual union all
select 'A/((B+C)+D)' expre1 from dual union all
select '((A/B)+C)+D' expre1 from dual union all
select '((A/B)+C)-D' expre1 from dual union all
select 'A/((B+C)-D)' expre1 from dual union all
select '((A/B)+C)/D' expre1 from dual union all
select '((A/B)-C)*D' expre1 from dual union all
select 'A/((B-C)+D)' expre1 from dual union all
select '((A/B)-C)+D' expre1 from dual union all
select 'A/((B-C)-D)' expre1 from dual union all
select '((A/B)-C)-D' expre1 from dual union all
select '((A/B)-C)/D' expre1 from dual union all
select 'A/((B/C)+D)' expre1 from dual union all
select 'A/((B/C)-D)' expre1 from dual
)
select expre1
       ,regexp_replace(expre1,'\(\(([ABCD])([\*/\+\-])([ABCD])\)([\*/\+\-])([ABCD])\)',
        case
          when '\4' = '+' or '\4' = '-' then '(\1\2\3\4\5)'
          else '((\1\2\3)\4\5)'
        end)  str
from temp
where regexp_instr(expre1,'\(\(([ABCD])([\*/\+\-])([ABCD])\)([\*/\+\-])([ABCD])\)') > 0;

bug结果如下:

EXPRE1                    STR
------------------------- ---------------
A*((B*C)+D)               A*((B*C)+D)
A*((B*C)-D)               A*((B*C)-D)
((A*B)+C)*D               ((A*B)+C)*D
((A*B)+C)+D               ((A*B)+C)+D
A*((B+C)+D)               A*((B+C)+D)
((A*B)+C)-D               ((A*B)+C)-D
A*((B+C)-D)               A*((B+C)-D)

看起来代码“when ' '”被视为字符串' '而不是正则表达式。我的目标是删除括号。那么,如何修改呢?

oracle case regexp-replace
1个回答
0
投票

也许无需正则表达式即可完成。使用您的样本数据 - 像这样

Select  EXPRE1,
        Case  When Instr(EXPRE1, '*') = 0 And Instr(EXPRE1, '/') = 0
                  Then '(' || Replace(Replace(EXPRE1, '(', ''), ')', '') || ')'
              When SubStr(EXPRE1, 4, 1) In('+', '-') And Instr(EXPRE1, '*', 1, 1) = Length(EXPRE1) - 1 And Instr(EXPRE1, '/') = 0
                  Then  '(' || 
                        Replace(
                          Replace(Replace(Replace(Replace(EXPRE1, '(', ''), ')', ''), ')+', '+'), ')-', '-')
                        , '*', ')*')
              When Instr(EXPRE1, '*', 1, 1) = 2 And Instr(EXPRE1, '*', 1, 2) = 0 And Instr(EXPRE1, '/') = 0
                  Then SubStr(EXPRE1, 1,  2) || '(' || Replace(Replace(SubStr(EXPRE1, 3), '(', ''), ')', '') || ')'
              When Instr(EXPRE1, '*', 1, 1) < Length(EXPRE1) - 1 And Instr(EXPRE1, '/') = 0
                  Then EXPRE1
              --
              When SubStr(EXPRE1, 4, 1) In('+', '-') And Instr(EXPRE1, '/', 1, 1) = Length(EXPRE1) - 1 And Instr(EXPRE1, '*') = 0
                  Then  '(' || 
                        Replace(
                          Replace(Replace(Replace(Replace(EXPRE1, '(', ''), ')', ''), ')+', '+'), ')-', '-')
                        , '/', ')/')
              When Instr(EXPRE1, '/', 1, 1) = 2 And Instr(EXPRE1, '/', 1, 2) = 0 And Instr(EXPRE1, '*') = 0
                  Then SubStr(EXPRE1, 1,  2) || '(' || Replace(Replace(SubStr(EXPRE1, 3), '(', ''), ')', '') || ')'
              --
              When Length(EXPRE1) - Length(Replace(EXPRE1, '*')) > 1 OR Length(EXPRE1) - Length(Replace(EXPRE1, '/')) > 1
                  Then EXPRE1
        Else  '((' || 
              SubStr(Replace(EXPRE1, '((', ''), 1, InStr(Replace(EXPRE1, '((', ''), ')', 1, 1)) ||
              SubStr(Replace(EXPRE1, '((', ''), InStr(Replace(EXPRE1, '((', ''), ')', 1, 1) + 1)
        End "STR"
From    temp
/*    R e s u l t :
EXPRE1      STR                                     
----------- ----------------------------------------
A*((B*C)+D) A*((B*C)+D)                             
A*((B*C)-D) A*((B*C)-D)                             
((A*B)+C)*D ((A*B)+C)*D                             
((A*B)+C)+D ((A*B)+C)+D                             
A*((B+C)+D) A*(B+C+D)                               
((A*B)+C)-D ((A*B)+C)-D                             
A*((B+C)-D) A*(B+C-D)                               
((A*B)+C)/D ((A*B)+C)/D                             
((A*B)-C)*D ((A*B)-C)*D                             
((A*B)-C)+D ((A*B)-C)+D                             
A*((B-C)+D) A*(B-C+D)                               
A*((B-C)-D) A*(B-C-D)                               
((A*B)-C)-D ((A*B)-C)-D                             
((A*B)-C)/D ((A*B)-C)/D                             
A*((B/C)+D) ((A*B/C)+D)                             
A*((B/C)-D) ((A*B/C)-D)                             
A+((B*C)+D) A+((B*C)+D)                             
A+((B*C)-D) A+((B*C)-D)                             
((A+B)+C)*D (A+B+C)*D                               
A+((B+C)+D) (A+B+C+D)                               
((A+B)+C)+D (A+B+C+D)                               
A+((B+C)-D) (A+B+C-D)                               
((A+B)+C)-D (A+B+C-D)                               
((A+B)+C)/D (A+B+C)/D                               
((A+B)-C)*D (A+B-C)*D                               
A+((B-C)+D) (A+B-C+D)                               
((A+B)-C)+D (A+B-C+D)                               
((A+B)-C)-D (A+B-C-D)                               
A+((B-C)-D) (A+B-C-D)                               
((A+B)-C)/D (A+B-C)/D                               
A+((B/C)+D) ((A+B/C)+D)                             
A+((B/C)-D) ((A+B/C)-D)                             
A-((B*C)+D) A-((B*C)+D)                             
A-((B*C)-D) A-((B*C)-D)                             
((A-B)+C)*D (A-B+C)*D                               
A-((B+C)+D) (A-B+C+D)                               
((A-B)+C)+D (A-B+C+D)                               
A-((B+C)-D) (A-B+C-D)                               
((A-B)+C)-D (A-B+C-D)                               
((A-B)+C)/D (A-B+C)/D                               
((A-B)-C)*D (A-B-C)*D                               
A-((B-C)+D) (A-B-C+D)                               
((A-B)-C)+D (A-B-C+D)                               
A-((B-C)-D) (A-B-C-D)                               
((A-B)-C)-D (A-B-C-D)                               
((A-B)-C)/D (A-B-C)/D                               
A-((B/C)+D) ((A-B/C)+D)                             
A-((B/C)-D) ((A-B/C)-D)                             
A/((B*C)+D) ((A/B*C)+D)                             
A/((B*C)-D) ((A/B*C)-D)                             
((A/B)+C)*D ((A/B)+C)*D                             
A/((B+C)+D) A/(B+C+D)                               
((A/B)+C)+D ((A/B)+C)+D                             
((A/B)+C)-D ((A/B)+C)-D                             
A/((B+C)-D) A/(B+C-D)                               
((A/B)+C)/D ((A/B)+C)/D                             
((A/B)-C)*D ((A/B)-C)*D                             
A/((B-C)+D) A/(B-C+D)                               
((A/B)-C)+D ((A/B)-C)+D                             
A/((B-C)-D) A/(B-C-D)                               
((A/B)-C)-D ((A/B)-C)-D                             
((A/B)-C)/D ((A/B)-C)/D                             
A/((B/C)+D) A/((B/C)+D)                             
A/((B/C)-D) A/((B/C)-D)                           */

© www.soinside.com 2019 - 2024. All rights reserved.