Given your input constraints, this regexp gives the desired result:

    s/(.)\1*/$1 eq 'M' ? 1 + pos . '-' : (pos || '') . $1/eg;

I don't believe your input strings are allowed to end with 'M', but if they are, you can use the slightly longer:

    s/((.)\2*)/$2 eq 'M' ? 1 + pos . '-' . (length($1) + pos) : $2/eg;

Edit (and N.B.): The above are complete statements that modify $_, not RHS fragments for use with =~. Refer to the very first line of pos for the reason behind this restriction:

Returns the offset of where the last m//g search left off for the variable in question ($_ is used when the variable is not specified).

To use these substitutions on a variable besides $_ (as in $string =~ s/.../.../eg; ), replace occurrences of pos with pos($string), or use the slightly more line-noisy @+/@- (@LAST_MATCH_(END|START)) available since Perl 5.6:

    $s =~ s/(.)\1*/$1 eq 'M' ? $+[1] . '-' : ($-[1] || '') . $1/eg;

In reply to Re: How to format such a string? by rjt
in thread How to format such a string? by Anonymous Monk

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.