0

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

MarkokraM
  • 980
  • 1
  • 12
  • 26
  • You need to use recursion to match nested brackets. – Barmar Jan 20 '17 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. – Barmar Jan 20 '17 at 18:41
  • In JS, use `XRegExp.matchRecursive(str, '\\(', '\\)', 'g')` (see [this answer](http://stackoverflow.com/a/40279904/3832970)). – Wiktor Stribiżew Jan 20 '17 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. – twalberg Jan 20 '17 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. – MarkokraM Jan 27 '17 at 14:35

0 Answers0