Search code examples
regex

Regular expression to match balanced parentheses


I need a regular expression to select all the text between two outer brackets.

Example:
START_TEXT(text here(possible text)text(possible text(more text)))END_TXT
^ ^

Result:
(text here(possible text)text(possible text(more text)))


Solution

  • Regular expressions are the wrong tool for the job because you are dealing with nested structures, i.e. recursion.

    But there is a simple algorithm to do this, which I described in more detail in this answer to a previous question. The gist is to write code which scans through the string keeping a counter of the open parentheses which have not yet been matched by a closing parenthesis. When that counter returns to zero, then you know you've reached the final closing parenthesis.