I think an s///e approach like Tux's s_ger() here is probably best for readability/maintainability, but just for grins, here's a "pure" regex approach:

Win8 Strawberry 5.8.9.5 (32) Sun 06/05/2022 5:38:06 C:\@Work\Perl\monks >perl use strict; use warnings; my $s = 'AAAAAXXXXXXXAAAXXXXXAXXXXAAAA'; print "'$s' \n"; $s =~ s{ (?: \G (?! \A) | (?<= A)) X (?= X* A) }{a}xmsg; print "'$s' \n"; ^Z 'AAAAAXXXXXXXAAAXXXXXAXXXXAAAA' 'AAAAAaaaaaaaAAAaaaaaAaaaaAAAA'
Please see perlre, perlretut, perlreref, and perlrequick.

Update: And a shameless elaboration:

Win8 Strawberry 5.8.9.5 (32) Sun 06/05/2022 6:52:29 C:\@Work\Perl\monks >perl use strict; use warnings; my %replace = map { 'X' x $_ => 'a' x $_ } 1, 3, 7; my ($rx_search) = map qr{ $_ }xms, join ' | ', map quotemeta, reverse sort keys %replace ; # print "\$rx_search $rx_search \n"; # for debug for my $s ( 'AAAAAXXXXXXXAAAXXXXXAXXXXAAAA', 'XXXXXXXAAAXXXXXAXXXX', 'AXA', 'XXXXXXXXXXX', 'AAAAAAAAAAA', 'XA', 'AX', 'X', 'A', '', ) { print "'$s' \n"; (my $t = $s) =~ s{ (?: \G (?! \A) | (?<= A)) ($rx_search) (?= X* A +) } {$replace{$1}}xmsg; print "'$t' \n\n"; } ^Z 'AAAAAXXXXXXXAAAXXXXXAXXXXAAAA' 'AAAAAaaaaaaaAAAaaaaaAaaaaAAAA' 'XXXXXXXAAAXXXXXAXXXX' 'XXXXXXXAAAaaaaaAXXXX' 'AXA' 'AaA' 'XXXXXXXXXXX' 'XXXXXXXXXXX' 'AAAAAAAAAAA' 'AAAAAAAAAAA' 'XA' 'XA' 'AX' 'AX' 'X' 'X' 'A' 'A' '' ''


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


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