简体   繁体   中英

Oracle REGEXP_SUBSTR not working with my pattern

I have the following query:

SELECT DISTINCT A.REZ FROM
(
  SELECT REGEXP_SUBSTR(P_EQUATION, '([A-Z|a-z|0-9]+)\{([0-9|\+|\-| |\*|\/\)\(]+)\}#([A-Z|a-z|0-9|_]+)#',1, LEVEL) AS REZ FROM DUAL
  CONNECT BY REGEXP_SUBSTR(P_EQUATION, '([A-Z|a-z|0-9]+)\{([0-9|\+|\-| |\*|\/\)\(]+)\}#([A-Z|a-z|0-9|_]+)#',1, LEVEL) IS NOT NULL
) A;

If I supplied the following input:

P_EQUATION := 'A123{(01+02)*2}#ACCOUNT_BALANCE# + B123{(20+10)/20}#ACCOUNT_BALANCE#';

It gives me the following:

REZ
-------------------------------------
A123{(01+02)*2}#ACCOUNT_BALANCE#
B123{(20+10)/20}#ACCOUNT_BALANCE#

But, although the minus sign is included in the pattern, if I have added it inside the curly brackets, it will not recognize the text anymore as a match!

ex:

P_EQUATION := 'A123{(01-02)*2}#ACCOUNT_BALANCE#';

I'm not able to find a solution to this, it is freaking me out, especially, when I tried to match the minus sign alone it works, if I tried to match digits alone it also works :(

Oracle appears to be using POSIX style regexes: https://docs.oracle.com/cd/B12037_01/server.101/b10759/ap_posix001.htm#i690819

The backslash is NOT a metacharacter in a POSIX bracket expression. So in POSIX, the regular expression [\\d] matches a \\ or a d

> http://www.regular-expressions.info/posixbrackets.html

The backslashes are probably messing it up, and they're not necessary. You also don't realize that | is a literal inside a char class (which the comments also pointed out). I have fixed these problems, and I moved the - to the start of the char class, which allows it to be interpreted as a literal.

Here you go:

([A-Za-z0-9]+)\{([-0-9+ */)(]+)\}#([A-Za-z0-9_]+)#

Couldn't quite figure out the issue with your code but here is one way to do it:

with temp as
(
  select 'A123{(01+02)*2}#ACCOUNT_BALANCE# + B123{(20+10)/20}#ACCOUNT_BALANCE#' P_EQUATION from dual union all
  select 'A123{(01-02)*2}#ACCOUNT_BALANCE#' P_EQUATION from dual
)

SELECT DISTINCT A.REZ FROM
(
  SELECT REGEXP_SUBSTR(P_EQUATION, '[[:alpha:]]+[[:digit:]]+{\([[:digit:]]+\S[[:digit:]]+\)\S[[:digit:]]+}#[[:alpha:]]+_[[:alpha:]]+#',1, LEVEL) AS REZ FROM temp
  CONNECT BY REGEXP_SUBSTR(P_EQUATION, '[[:alpha:]]+[[:digit:]]+{\([[:digit:]]+\S[[:digit:]]+\)\S[[:digit:]]+}#[[:alpha:]]+_[[:alpha:]]+#',1, LEVEL)  IS NOT NULL
) A;

OUTPUT:

REZ                                                                                                                                                                                                                                                                            
---------------------------------------
B123{(20+10)/20}#ACCOUNT_BALANCE#                                                                                                                                                                                                                                                
A123{(01-02)*2}#ACCOUNT_BALANCE#                                                                                                                                                                                                                                                 
A123{(01+02)*2}#ACCOUNT_BALANCE# 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM