sub substitute { my $message = shift; my @numeric_matches = ($message =~ m/\{\\d\d+\}/g); foreach (@numeric_matches) { my $substitution = $_; my ($substitution_count) = (substitution =~ m/(\d+)/); my $number; for (1..$substitution_count) { $number .= $numeric_chars[int rand @numeric_chars]; } $substitution =~ s/\\/\\\\/g; $message =~ s/$substitution/$number/ee; } return $message; }