Your result only depend on the number of words before your substring and the number of words in the substring:
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 mat +ch my $m = () = $substr =~ /\b(\w+)\b/g; # number of words in match print join(' ',$n+1..$n+$m),"\n"; }
In reply to Re: Find the position of substring
by hdb
in thread Find the position of substring
by Anonymous Monk
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |