my ($nobrackets) = (qr/[^\{^\}]+/); &replace(qr/\{$nobrackets\}/,'$1') #This line is changed below. sub replace{ s/$_[1]/$_[2]/g }