If you have only one level of parentheses, then there are two possibilities.
Option 1: use repeat repetition:
/\(.*?\)/
This will stop when he meets the first ) .
Option 2: use the negative character class
/\([^)]*\)/
This can only repeat characters that are not ) , so it may never go past the first closing parenthesis. This option is usually preferred for performance reasons. Also, this option is easier to extend to avoid brackets being accelerated (so that you can match this full line: (some\)thing) instead of throwing thing) away thing) ). But this is probably quite rarely necessary.
However, if you want nested structures, this is usually too complicated for regular expression (although some options, such as PCRE, support recursive patterns). In this case, you just need to go through the line and count the parentheses to track the current level of nesting.
Just like a side note about these recursive patterns: In PCRE (?R) , the whole pattern is simply displayed, so pasting this place makes it all recursive. But then each content of parentheses should be the same structure as the whole match. In addition, it is actually impossible to make meaningful one-step replacements for this, as well as to use capture groups on several nested levels. In general, itβs best for you not to use regular expressions for nested structures.
Update:. Since it seems to you that you are looking for a solution for regular expressions, here is how you would match your example using PCRE (PHP implementation example):
$str = 'there are (many (things (on) the)) box (except (carrots (and apples)))'; preg_match_all('/\([^()]*(?:(?R)[^()]*)*\)/', $str, $matches); print_r($matches);
leads to
Array ( [0] => Array ( [0] => (many (things (on) the)) [1] => (except (carrots (and apples))) ) )
What the template does:
\(
This allows you to use infinite nested levels, as well as for infinite parallel levels.
Note that it is not possible to get all nested levels as separate captured groups. You will always receive only the largest or outermost group. In addition, it is not possible to make a recursive replacement.