The very first thing to do in a case like this is to write a test plan, then write a whole bunch of tests. I leave all the rest of the tests to you.

Win8 Strawberry 5.8.9.5 (32) Wed 09/07/2022 21:20:21 C:\@Work\Perl\monks >perl use strict; use warnings; use Test::More; use Test::NoWarnings; # use Data::Dump qw(dd); # for debug my @Tests = ( 'ALL these strings have replacements', [ 'UUUUUUUUIIIIIIIIIIIIIIIMMMMMMMMMMMMMMMMMMMMMMMMOOOOO', 'IIIIIIIIIIIIIIIIIIIIIIIMMMMMMMMMMMMMMMMMMMMMMMMOOOOO', ], [ 'IIIIIIIIIIIIIIIMMMMMMMMMMMMMMMMMMMMMMMMOOOOOUUUUUUUU', 'IIIIIIIIIIIIIIIMMMMMMMMMMMMMMMMMMMMMMMMOOOOOOOOOOOOO', ], [ 'IIIIIIIIIIIIIIIMMMMMMMMMMMUUUUUUUUMMMMMMMMMMMMMOOOO', 'IIIIIIIIIIIIIIIMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMOOOO', ], [ 'IIIIIIIUUUUUIIIIIIIIMMMMMMMMMMMMMMMMMMMMMMMMOOUUUUUUUOO', 'IIIIIIIIIIIIIIIIIIIIMMMMMMMMMMMMMMMMMMMMMMMMOOOOOOOOOOO', ], [ 'IIIIIIIIIIIIIIIMMMMMMMMMMMMMMMMMMMMMMMMOOUUOO', 'IIIIIIIIIIIIIIIMMMMMMMMMMMMMMMMMMMMMMMMOOOOOO', ], 'NONE of these strings have replacements', [ ('IIIIIIIIIIIIIIIMMMMMMMMMMMMMMMMMMMMMMMMMMUUOO') x 2 ], ); # end @Tests my @additional = qw(Test::NoWarnings); # each of these adds 1 test plan 'tests' => (scalar grep { ref eq 'ARRAY' } @Tests) + @additional; VECTOR: for my $ar_vector (@Tests) { if (not ref $ar_vector) { note $ar_vector; next VECTOR; } my ($input, $expected) = @$ar_vector; my $got = replace($input); is $got, $expected, "'$input' \n '$got'" ; } # end for VECTOR sub replace { # works my ($str, ) = @_; # only ONE or NONE of these replacements will be made. $str =~ s{ \A (U+) (?= ([IOPBM])) } { $2 x length $1 }xmse o +r $str =~ s{ (?<= ([IO])) (U+) \z } { $1 x length $2 }xmse o +r $str =~ s{ (?<= ([IOPBM])) (U+) (?= \1) }{ $1 x length $2 }xmseg ; return $str # return string with possible replacements } # end sub replace() ^Z 1..7 # ALL these strings have replacements ok 1 - 'UUUUUUUUIIIIIIIIIIIIIIIMMMMMMMMMMMMMMMMMMMMMMMMOOOOO' # 'IIIIIIIIIIIIIIIIIIIIIIIMMMMMMMMMMMMMMMMMMMMMMMMOOOOO' ok 2 - 'IIIIIIIIIIIIIIIMMMMMMMMMMMMMMMMMMMMMMMMOOOOOUUUUUUUU' # 'IIIIIIIIIIIIIIIMMMMMMMMMMMMMMMMMMMMMMMMOOOOOOOOOOOOO' ok 3 - 'IIIIIIIIIIIIIIIMMMMMMMMMMMUUUUUUUUMMMMMMMMMMMMMOOOO' # 'IIIIIIIIIIIIIIIMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMOOOO' ok 4 - 'IIIIIIIUUUUUIIIIIIIIMMMMMMMMMMMMMMMMMMMMMMMMOOUUUUUUUOO' # 'IIIIIIIIIIIIIIIIIIIIMMMMMMMMMMMMMMMMMMMMMMMMOOOOOOOOOOO' ok 5 - 'IIIIIIIIIIIIIIIMMMMMMMMMMMMMMMMMMMMMMMMOOUUOO' # 'IIIIIIIIIIIIIIIMMMMMMMMMMMMMMMMMMMMMMMMOOOOOO' # NONE of these strings have replacements ok 6 - 'IIIIIIIIIIIIIIIMMMMMMMMMMMMMMMMMMMMMMMMMMUUOO' # 'IIIIIIIIIIIIIIIMMMMMMMMMMMMMMMMMMMMMMMMMMUUOO' ok 7 - no warnings
See also How to ask better questions using Test::More and sample data.


Give a man a fish:  <%-{-{-{-<


In reply to Re: Replace characters within string by AnomalousMonk
in thread Replace characters within 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.