1

Is it possible to capture nested parenthesis so that group is concluded on same level that it was started. For example:

(A AND (B)) or (C and (D or E) or (F) and G) ->

Should become groups:

1 (A AND (B))
2 (C and (D or E) or (F) and G) 

But at the moment this regex doesn't quite do the job:

\([^\(](?:[^\)])*\)

http://www.regexr.com/3f42a

5
  • You need to use recursion to match nested brackets. Commented Jan 20, 2017 at 18:39
  • What language are you using? The specific solution depends on the flavor of regular expressions. If you search SO for [regex] nested parentheses you'll find lots of previous answers for different languages. Commented Jan 20, 2017 at 18:41
  • In JS, use XRegExp.matchRecursive(str, '\\(', '\\)', 'g') (see this answer). Commented Jan 20, 2017 at 18:44
  • With true regular expressions (by the computer science or theoretical definition), this is not possible. However, with some of the extended "regular expression" families that are no longer truly regular expressions (i.e. perl's PCREs) it can be done. Commented Jan 20, 2017 at 19:58
  • Thanks for clarification. Subject was discussed a lot on other topics, closing justified. But link should point to resource given by Wiktor on comment. Not to the general regex topic. Commented Jan 27, 2017 at 14:35

0

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.