in reply to Find the position of substring
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"; }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Find the position of substring
by Anonymous Monk on Apr 22, 2015 at 12:28 UTC |