I have a scenario where I have a string column containing nested brackets and I need to extract the strings from that string in a recursive manner.

eg1: A1 AND (A4 OR (A2 AND A3)) AND (B1 OR B2) should be splitted into

  1. A2 AND A3->logic1
  2. A4 OR logic1 ->logic2
  3. B1 OR B2 ->logic3
  4. logic2 AND logic3 ->logic4
  5. A1 AND logic4 ->logic5

eg2: (A2 AND A3) OR (B1 AND B2) should be splitted into

  1. B1 AND B2->logic1
  2. A2 AND A3->logic2
  3. logic1 OR logic2->logic3

So far, I tried using split function (split based on ‘(‘ in spark sql) and used dense_rank() based on no of brackets in the string

with t1 as 
(select '(A2 AND A3) OR (B1 AND B2)' as logic from table limit 1
)
,t1_exploded as 
(
select logic,explode(split(logic,'\(')) as logic_splitted from t1
)
,t1_logic_splitted as
(
  select logic,trim(logic_splitted) as logic_splitted,
dense_Rank() over (order by 
len(logic_splitted) - len(replace(logic_splitted,')','')) desc,logic_splitted desc)
as logic_group from t1_exploded
)
select * from t1_logic_splitted

But I am facing issues when there are more than 2 AND/OR.
Please let me know if there is any better options and thank you very much for the help