here is my string A B C D
and I want to replace A
by 123
and C
by 456
for example. However this doesn't work.
$string=~ s/A|B|C|D/123|B|456|D/;
I want this 123 B 456 D
but i get this 123|B|456|D B C D
Probably because the number of characters in my two patterns is different.
Is there a way to substitute patterns of different size using some other piece of code ? Thanks a lot.
Something like this using eval (untested).
Using the eval flag in the
s///
form means to evaluate the replacementside as a line of code that returns a value.
In this case it executes a ternary conditional in the replacement code.
It's sort of like an inline regex callback.
It's much more complicated though since it can be like
s///eeg
sobetter to refer to the docs.
Remember, eval is really evil, misspelled !!