1
\(([^\\(\\)]+)\)

My regex above captures everything between each set of brackets of the form

(Hello OR there) AND (big AND wide AND world)

I get

Hello OR there
big AND wide AND world

But it falls down when there are parentheses inside the terms within the brackets

(Hello OR there AND messing(it)up) AND (big AND wide AND world)

Returns

it
big AND wide AND world

Whereas I want

Hello OR there AND messing(it)up
big AND wide AND world

I'm not sure if that's possible with regex or what the best approach would be?

Paolo
  • 21,270
  • 6
  • 38
  • 69
user1561108
  • 2,666
  • 9
  • 44
  • 69

1 Answers1

1

You may use the following pattern:

\(((?:[^()]+|(?R))*+)\)

The (?R) subexpression recurses the entire pattern if possible.

You may try it here.


For input:

(Hello OR there AND messing(it)up) AND (big AND wide AND world)

Captured groups are:

Group 1.    47-79   `Hello OR there AND messing(it)up`
Group 1.    86-108  `big AND wide AND world`

If you are working in Python, you can use the regex module:

import regex

mystring = '(Hello OR there AND messing(it)up) AND (big AND wide AND world)'
print(regex.findall('(?V1)\(((?:[^()]+|(?R))*+)\)',mystring))

Prints:

['Hello OR there AND messing(it)up', 'big AND wide AND world']
Paolo
  • 21,270
  • 6
  • 38
  • 69