use strict; use warnings; my @strings = qw{ ccaatTTTGACACACACAGAAgggca --aatTTTGACACACACAGAAgggca --aatTTTGACACACACAGAA ---aagctaagattca TTTGACACACACAGAAgggca ---TTTGACACACACAGAAgggca }; foreach my $string ( @strings ) { my ($sp, $ep) = ucRange($string); print qq{ String - $string\n}, qq{Start position - $sp\n}, qq{ End position - $ep\n\n}; } sub ucRange { local our $str = shift; $str =~ s{\A-*}{_}; local our $startPos = 0; $str =~ m{(?<=[a-z_])(?=[A-Z])(?{$startPos = pos $str})}; local our $endPos = 0; $str =~ m{(?<=[A-Z])(?=[A-Z](?:[a-z]|\z))(?{$endPos = pos $str})}; return ($startPos, $endPos); }