perl -wle '$foo = "a hello w"; $foo = $foo =~ s/(?=.*(hello)).*/$1/sr; print $foo' #### hello #### ...; print $foo =~ s/(?=.*(hello)).*/$1/sr; #### ...; $foo =~ /hello(?{ $foo = $& })/; print $foo