$text = "my name is john q user\n"; $text =~ s/^my name is (.*?) .*$/$1/; # removes "my name is ", saves the next word, essentially, into $1, removes the rest print "hello, $text!\n"; # prints "hello, john!\n"