1

I am trying to find all the top level 'except' clauses from the below string.

where (
 param1 equals value1
 and
 param 2 equals valu2
 except (
  param3 equals value3
  where (
   param4 equals value4
   except (
     param6 equals value6
   )
  )
 )
)
except (
 param5 equal value5
)

Expected Result:

except (
  param3 equals value3
  where (
   param4 equals value4
   except (
     param6 equals value6
   )
  )
 )

&

 except (
     param5 equal value5
    )

I am new to regex and I was trying the below regex but its not working. except.((?:[^()]+|(?R))+)

Demo: https://regex101.com/r/AJZ1MV/1

5
  • You need to recurse Group 1 after adding parentheses around \(...\), except.*(\((?:[^()]++|(?1))*+\)) Commented Sep 14, 2022 at 9:47
  • Thanks! This works fine in regex editor but not working with Orracle SQL REGEXP_SUBSTR. Any leads? Commented Sep 27, 2022 at 10:11
  • You must always provide the language where you are using the regex. Of course, Oracle POSIX based regex engine does not support lookarounds, nor does it support recursion and subroutines. Commented Sep 27, 2022 at 10:14
  • Yeah Thats my bad! Could you suggest any work around? Here is the sqlfidle sqlfiddle.com/#!4/13ba6c/4 Commented Sep 27, 2022 at 10:31
  • No, you cannot use a regex for this. You need a whole parser. Commented Sep 27, 2022 at 10:34

1 Answer 1

0

This regex groups the cases you mentioned but must be improved:

(?:(?<=[\s])(except (?:.|\s)*)(?=except|\)))?|(?:(?<=\n)(except (?:.|\s)*)(?=$|\)))

Demo: https://regex101.com/r/20RAQV/1

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for your response! This looks good in demo but not working with Oracle SQL. Actually I wanted to use it with SQL REGEXP_SUBSTR.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.