$_ = '(($a $a hi) $a ($a) $a)'; my $re; $re = qr/\((?:(?>[^()]*)|(??{$re}))*\)/; s/($re)|\$a/$1?$1:'$b'/eg; print; __DATA__ (($a $a hi) $b ($a) $b)