in reply to index with regex
Update: pos, as btrott points out, is the right answer. My idea won't find multiple instances of your search string per target string (and besides, why re-invent a perfectly round wheel?).
Prints:my $R = 'Russ Ethan Jason Eric'; $R =~ /Ethan/; print index($R, $&), "\n"; $R =~ /Eric/; print index($R, $&), "\n";
So, adapting my first code to the better answer, this is how I might look in multiple strings for multiple patterns:
Prints:my @R = ('Russ Ethan Jason Eric JAPH', 'JAPH vroom Ozymandias neshura +Russ'); for (my $i = 0; $i != @R; $i++){ while ($R[$i] =~ /Russ|JAPH/g){ print "Found $& in string $i at: ", pos($R[$i]) - length $&, "\n"; } }
Russ
Brainbench 'Most Valuable Professional' for Perl
|
---|