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
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |