Re: s/A/B/ and s/B/C/ but don't want A -> C



Abigail schreef:

my %replacement = (A => B, B => C);
s/(A|B)/$replacement{$1}/g;

If string-A can ever be a subpattern of string-B, then you need to add a
"longest first" approach.

$ echo "AABABA" | perl -wpe '
BEGIN{ %repl = (A => B, AB => C); }
s/(A|AB)/$repl{$1}/g;
'
BBBBBB

$ echo "AABABA" | perl -wpe '
BEGIN{ %repl = (A => B, AB => C); }
s/(AB|A)/$repl{$1}/g; # longest first
'
BCCB

(or use a different regex-engine :)

--
Affijn, Ruud

"Gewoon is een tijger."

.