$ perl -le' $_ = "GAGAGACCCCGATCGAGAGACCCGATCFGAGAVCTGATCCCC"; my $substring = "GATC"; print "length of string is ", length; my $start = 0; while ( /$substring/g ) { print qq/"$substring" found at $-[0] to $+[0], distance from last is /, $-[0] - $start; $start = $+[0]; } print "distance from end is ", length() - $start; ' length of string is 42 "GATC" found at 10 to 14, distance from last is 10 "GATC" found at 23 to 27, distance from last is 9 "GATC" found at 35 to 39, distance from last is 8 distance from end is 3