use strict; use warnings; my $string = "word1 word2 word3"; $string =~ s/(\w+)(?=\W|$)/ucfirst $1/eg;