in reply to Substitution is replacing one character set out of two

A lookahead should do the trick:

use strict; use warnings; use Test::More tests => 1; my $have = 'EST07|E9028;CP392;ALLOC_CP392|R2021||||||||||||0.46458'; my $want = 'EST07|E9028;CP392;ALLOC_CP392|R2021|#MI|#MI|#MI|#MI|#MI|#M +I|#MI|#MI|#MI|#MI|#MI|0.46458'; $have =~ s/\|(?=\|)/|#MI/g; is $have, $want;

🦛

Replies are listed 'Best First'.
Re^2: Substitution is replacing one character set out of two
by sroux (Sexton) on Apr 01, 2022 at 11:37 UTC
    Thank you for the workaround but why such a behavior?

      Without the lookahead your search and replace consumes the second pipe, so subsequent attempts to match will start beyond it. I would not call it a workaround - it's a valid solution to the task.


      🦛

        Crystal clear, thank you!