use Regexp::Common; ... $content =~ s{ $rx_target \s+ [[:alpha:]]+ \s* $RE{balanced}{-parens=>'{}'} \s* } {}xmsg; ...