Home > Software engineering >  Regex Match splitting in phrases based on Operators AND and OR
Regex Match splitting in phrases based on Operators AND and OR

Time:09-03

I have this input and i want to split using regex in the following form:

Original

procedure-type=(C OR B) AND notice-type=(cn-standard OR can-standard) OR nature=(3 OR 9|Z OR services)

Desired outcome

procedure-type=(C OR B)
notice-type=(cn-standard OR can-standard) 
nature=(3 OR 9|Z OR services)

If i use a normal split based on AND, OR it will not work because the procedure-type=(C OR B) will be splitted as well which is something i dont want.

Note: It might also have an assignment without clauses for instance procedure-type=C

Any ideas?

CodePudding user response:

Assuming, as per the comment-section, no nested paranthesis; have a try with:

\s (?:AND|OR)\s (?=(?:[^()]*\([^()]*\))*[^()]*$)

See an online demo

  • \s (?:AND|OR)\s - Literally 'AND' or 'OR' with 1 whitespaces;
  • (?= - Open positive lookahead;
    • (?:[^()]*\([^()]*\))* - A non-capture group matched 0 times capturing 0 characters other than paranthesis followed by open- and closing paranthesis with 0 non-paranthesis in between;
    • [^()]*$ - Match 0 characters other than paranthesis before end-line anchor.
  • Related