use strict; use warnings; my $str = 'aaAabBAAa'; my $N = 2; print join "\n", grep {/^(.)\1+$/i} map {substr $str, $_, $N} 0 .. length ($str) - $N;