To cope with the variable leading hyphens and the counting from 1 rather than 0 I decided to substitute zero or more hyphens at the beginning of the string with a single underscore to get the position as the OP wanted. I also used look arounds and regex code blocks. This caused me problems until I realised that the code blocks had created closures around $str, $startPos and $endPos when they were lexical. Declaring them with local our got things working.

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); }

The output.

String - ccaatTTTGACACACACAGAAgggca Start position - 6 End position - 21 String - --aatTTTGACACACACAGAAgggca Start position - 4 End position - 19 String - --aatTTTGACACACACAGAA Start position - 4 End position - 19 String - ---aagctaagattca Start position - 0 End position - 0 String - TTTGACACACACAGAAgggca Start position - 1 End position - 16 String - ---TTTGACACACACAGAAgggca Start position - 1 End position - 16

I hope this is of interest.

Cheers,

JohnGG

Update: Added string with no uppercase to check that script handled that.


In reply to Re: Finding Start/End Position of the Uppercase Substring by johngg
in thread Finding Start/End Position of the Uppercase Substring by neversaint

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.