use strict; use warnings; my $string = 'this is a test'; my $substr = 'is a test'; if( $string =~ /(.*)\b$substr\b/ ) { # do we have a match? my $n = () = $1 =~ /\b(\w+)\b/g; # number of words before match my $m = () = $substr =~ /\b(\w+)\b/g; # number of words in match print join(' ',$n+1..$n+$m),"\n"; }