my @nums = qw/ 3 9 2 9 /;; { my $re = join '|', map { "\Q$_\E" } @nums; my $str = 'and a 3 9 2 9 '; $str =~ s{($re)}{"$1"}g; print "$str\n"; } { my $re = join '\s*', map { "\Q$_\E" } @nums; my $str = 'and a 3 9 2 9 '; $str =~ s{($re)}{"$1"}g; print "$str\n"; } __END__ and a "3" "9" "2" "9" and a "3 9 2 9"