$html =~ s/(.*?)/&change($1, $2, $3)/isge; must be $html =~ s/(.*?)<\/a>/&change($1, $2, $3)/isge;