my %xlate = ( '(' => '', ') ' => '-' ); $phone =~ s{ ([()][ ]?) }{$xlate{$1}}xmsg; # OR $phone =~ s{ \( \s* (\d{3}) \s* \) \s* }{$1-}xmsg;