use strict; use warnings; use Test::More tests => 2; my $pos = match_positions ('GCTTCTTGC', 'AGCTTCTTGCGCTTCTT'); is ($pos, '2 9 ', 'Matched'); # This fails because you have only one occurrence. $pos = match_positions ('GCG', 'AGCGCGT'); is ($pos, '2 4 ', 'Matched'); sub match_positions { my ($regexp, $sequence) = @_; my @positions = ( ); while ( $sequence =~ m/$regexp/ig ) { my $newpos = pos($sequence) - length($&) + 1; push @positions, $newpos; pos($sequence) = $newpos; } return "@positions "; }