> Not that you cannot see s+D+s+D+s+ sequence, or s+U+s+U+s+. D and U alternate.

If you mean "Note" by "Not", than your sample input is invalid, as both the sequences contain sDsDs.

Also, I think the following works correctly even for the non-alternating sequences in the way you originally showed:

my %before = (U => 'I', D => 'O'); my %after; @after{ keys %before } = reverse values %before; sub subst { local ($_) = @_; 1 while s/(s+)(?=([DU]))/$before{$2} x length $1/e | s/(?<=([DU]))(s+)/$after{$1} x length $2/e; tr/DU/MM/ or tr/s/I/; return $_ }
It uses the bitwise or that doesn't short circuit to try both the substitutions every time while there's anything to replace.

Tested against:

use Test::More; my %simple = (ss => 'II', ssUUss => 'IIMMOO', ssDDss => 'OOMMII', ssDDssUUss => 'OOMMIIMMOO', ssUUssDDss => 'IIMMOOMMII'); for my $input (sort keys %simple) { is subst($input), $simple{$input}, $input; }

Might need some tweaking if the specification changes.

map{substr$_->[0],$_->[1]||0,1}[\*||{},3],[[]],[ref qr-1,-,-1],[{}],[sub{}^*ARGV,3]

In reply to Re^5: Pattern matching simultaneous substitution by choroba
in thread Pattern matching simultaneous substitution 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.