How to find all occurrences of a string in another string using Perl?
#!/usr/bin/perl
use strict;
use warnings;
my $string = 'needle in the haystack. needle again. needle';
my $find_me = 'needle';
my $offset = 0;
my $result = index($string, $find_me, $offset);
while ($result != -1) {
print "Found $find_me at $result\n";
$offset = $result + 1;
$result = index($string, $find_me, $offset);
}
Related posts: