Solved

Oracle Regular Expression Looking for Missing Parenthesis

Posted on 2009-03-29
9
636 Views
Last Modified: 2013-12-18
I am looking to find missing parens, both right or left, in an Oracle column of varchar. So far I have

select  testcol,  'MISSING LEFT PAREN' NOTE
from test a
where
regexp_like(a.testcol, '[^(].*[)]')
;

What I need to do is find any missing parens that should be there to match an existing one. Either right or left and if there are multiples.
Thanks much,
0
Comment
Question by:Caputo
  • 6
  • 2
9 Comments
 
LVL 73

Accepted Solution

by:
sdstuber earned 500 total points
ID: 24013620
unfortunately this is not solvable for an unknown number of parentheses pairs with regular expressions (at least not most regexp engines including Oracle's)

if you know you will have at most N pairs, you can build a patterns to handle those N

if N is unknown though, then you will have to resort to other means


one relatively easy way to look for N pairs is strip all non-parentheses characters then replace all '()' strings with null N times
if you're left with a null string afterwards then you had good pairing, if you end up with non-null string then you had bad pairing

here's an example where N = 3.





SELECT   s,

         NVL2(

             REGEXP_REPLACE(

                 REGEXP_REPLACE(

                     REGEXP_REPLACE(REGEXP_REPLACE(s, '[^()]'), '\([^()]*\)'),

                     '\([^()]*\)'

                 ),

                 '\([^()]*\)'

             ),

             'Bad pairs',

             'Good pairs'

         )

  FROM   (SELECT   'nopairs' s FROM DUAL

          UNION ALL

          SELECT   'bad ( left' s FROM DUAL

          UNION ALL

          SELECT   'bad ) right' s FROM DUAL

          UNION ALL

          SELECT   '(good) and (bad' s FROM DUAL

          UNION ALL

          SELECT   'one (pair)' s FROM DUAL

          UNION ALL

          SELECT   '(two) (pairs)' s FROM DUAL

          UNION ALL

          SELECT   '((two pairs nested))' s FROM DUAL

          UNION ALL

          SELECT   '(three) ( ) (pairs)' FROM DUAL

          UNION ALL

          SELECT   '(((three pairs nested)))' FROM DUAL)

Open in new window

0
 
LVL 73

Expert Comment

by:sdstuber
ID: 24013818
the expressions above can be simplified with ...


SELECT   s,
         NVL2(
            REGEXP_REPLACE(REGEXP_REPLACE(REGEXP_REPLACE(REGEXP_REPLACE(s, '[^()]'), '\(\)'), '\(\)'),'\(\)'),
             'Bad pairs',
             'Good pairs'
         )



here's a function that will iterate through any number of pairs
if it returns NULL then your string had good pairs, if it returns not-null then you have uneven pairs
CREATE OR REPLACE FUNCTION good_pairs(p_string IN VARCHAR2)

    RETURN VARCHAR2

IS

    v_temp   VARCHAR2(32767) := REGEXP_REPLACE(p_string, '[^()]', NULL);

    v_new    VARCHAR2(32767) := REGEXP_REPLACE(v_temp, '\(\)');

BEGIN

    WHILE v_temp != NVL(v_new, '-')

    LOOP

        v_temp   := v_new;

        v_new    := REGEXP_REPLACE(v_new, '\(\)');

    END LOOP;
 

    RETURN v_temp;

END;

Open in new window

0
 
LVL 32

Expert Comment

by:awking00
ID: 24019127
See attached.
test-parens.txt
0
 
LVL 73

Expert Comment

by:sdstuber
ID: 24019256
if I'm reading that right, doesn't test_parens only check the total count of left and right?

so  ' a)))(  b ( c ('   would check out ok
0
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
LVL 32

Expert Comment

by:awking00
ID: 24020972
sdstuber,
As usual, you're right. They do should be evaluated as pairs, which that function doesn't do. Back to the drawing board.
0
 
LVL 73

Expert Comment

by:sdstuber
ID: 24021540
here are two other versions that use a stack type algorithm,  push for ( and pop for )

version 2 iterates through all characters
version 3 strips all non parentheses characters first (using regular expression) then iterates through just the parentheses

version 3 is faster than version 2, but neither is as good as the regular expression looping from post 24013818  (for 110,000 iterations)

9 seconds for version 1
30 seconds for version 2,
14 seconds for version 3

but the best I found is the first post with the hard limit of 3,  
that ran in 531 milliseconds for 110,000 iterations

so, if you can identify a maximum number of parentheses to check I recommend doing it that way since invoking a pl/sql function is MUCH slower, even in the best case
0
 
LVL 73

Expert Comment

by:sdstuber
ID: 24021544
here are two other versions that use a stack type algorithm,  push for ( and pop for )

version 2 iterates through all characters
version 3 strips all non parentheses characters first (using regular expression) then iterates through just the parentheses

version 3 is faster than version 2, but neither is as good as the regular expression looping from post 24013818  (for 110,000 iterations)

9 seconds for version 1
30 seconds for version 2,
14 seconds for version 3

but the best I found is the first post with the hard limit of 3,  
that ran in 531 milliseconds for 110,000 iterations

so, if you can identify a maximum number of parentheses to check I recommend doing it that way since invoking a pl/sql function is MUCH slower, even in the best case
CREATE OR REPLACE FUNCTION good_pairs2(p_string IN VARCHAR2)

    RETURN VARCHAR2

IS

    v_cnt   INTEGER := 0;

    v_index INTEGER := 1;

BEGIN

    WHILE v_index <= LENGTH(p_string)

    LOOP

        IF SUBSTR(p_string, v_index, 1) = '('

        THEN

            v_cnt := v_cnt + 1;

        ELSIF SUBSTR(p_string, v_index, 1) = ')'

        THEN

            v_cnt := v_cnt - 1;

        END IF;
 

        IF v_cnt < 0

        THEN

            EXIT;

        END IF;
 

        v_index := v_index + 1;

    END LOOP;
 

    IF v_cnt = 0

    THEN

        RETURN 'Good Pairs';

    ELSE

        RETURN 'Bad Pairs';

    END IF;

END;
 
 

CREATE OR REPLACE FUNCTION good_pairs3(p_string IN VARCHAR2)

    RETURN VARCHAR2

IS

    v_cnt   INTEGER := 0;

    v_index INTEGER := 1;

    v_string varchar2(32767) := regexp_replace(p_string,'[^()]');

BEGIN

    WHILE v_index <= LENGTH(v_string)

    LOOP

        IF SUBSTR(v_string, v_index, 1) = '('

        THEN

            v_cnt := v_cnt + 1;

        ELSIF SUBSTR(v_string, v_index, 1) = ')'

        THEN

            v_cnt := v_cnt - 1;

        END IF;
 

        IF v_cnt < 0

        THEN

            EXIT;

        END IF;
 

        v_index := v_index + 1;

    END LOOP;
 

    IF v_cnt = 0

    THEN

        RETURN 'Good Pairs';

    ELSE

        RETURN 'Bad Pairs';

    END IF;

END;

Open in new window

0
 

Author Comment

by:Caputo
ID: 24029330
Thanks guys, I think the first answer will suffice, however the others are certainly interesting and worth some review.
0
 
LVL 73

Expert Comment

by:sdstuber
ID: 24029557
glad I could help
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Dataware house query tuning 9 64
Concat multi row values of a field in oracle 6 52
Determine Who is Runnig my Bash Shell Script 4 61
grouping on time windows 6 41
Subquery in Oracle: Sub queries are one of advance queries in oracle. Types of advance queries: •      Sub Queries •      Hierarchical Queries •      Set Operators Sub queries are know as the query called from another query or another subquery. It can …
Introduction A previously published article on Experts Exchange ("Joins in Oracle", http://www.experts-exchange.com/Database/Oracle/A_8249-Joins-in-Oracle.html) makes a statement about "Oracle proprietary" joins and mixes the join syntax with gen…
Via a live example, show how to take different types of Oracle backups using RMAN.
This video shows how to recover a database from a user managed backup

914 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now