in reply to How to find matching pairs

Also Regexp::Common would meet your needs.
use Regexp::Common; my $parens = qr/$RE{balanced}{-parens=>'()'}/; # ... # my fix $theParam =~ s/([A-Z]+)($parens)/&doFunc($1,$2)/geo; __END__ Using your script, it will print Example 1: func A returns <(func B returns <(2)>)> Example 2: func A returns <(func B returns <(2)>+func C returns <(func + D returns <()>)>)>