I am trying to workout a regular expression that can be used to parse logical expression with brackets
For example:
((weight gt 10) OR (weight lt 100)) AND (length lt 50)
I hope it can be parsed into:
Group 1: (weight gt 10) OR (weight lt 100)
Group 2: AND
Group 3: length lt 50
And if the order of this changes:
(length lt 50) AND ((weight gt 10) OR (weight lt 100))
I hope it can be parsed into:
Group 1: length lt 50
Group 2: AND
Group 3: (weight gt 10) OR (weight lt 100)
The costest I tried is this expression:
(\((?>[^()]+|(?1))*\))
The problem of this is that it only worked partially:
((weight gt 10) OR (weight lt 100)) AND (length lt 50)
Group 1: ((weight gt 10) OR (weight lt 100))
Group 2: (length lt 50)
(length lt 50) AND ((weight gt 10) OR (weight lt 100))
Group 1: (length lt 50)
Group 2: ((weight gt 10) OR (weight lt 100))
The logcal operator is not selected as a group.
How can I fix this to catch the logical operator AND?