Given the following input
$ cat pre stuff MACRO1 stuff MACRO2 stuff MACRO2 stuff MACRO1 stuff MACRO2 stuff
I want to replace MACRO2 (with MACRO3) if MACRO1 also exists. For instance:
$ perl -ne '/(?=.*MACRO1).*MACRO2/ ? print s/MACRO2/MACRO3/gr : print' pre stuff MACRO1 stuff MACRO3 stuff MACRO3 stuff MACRO1 stuff MACRO2 stuff
(I suppose .*MACRO2 part of this expression is not needed, now that I think about it) Edit. Less silly version above based on feedback:
$ perl -ne '/MACRO1/ ? print s/MACRO2/MACRO3/gr : print' pre
What I'm trying to figure out is how to do this with regex only. Here is one try:
$ perl -ne 'print s/(?=.*MACRO1)(?=.*MACRO2)MACRO2/MACRO3/gr' pre stuff MACRO1 stuff MACRO2 stuff MACRO3 stuff MACRO1 stuff MACRO2 stuff
I think that I have a fundamental confusion as to how the gaze operator can be both an โanchorโ and a โnon-consumingโ one. If I think of ?= As an anchor, it seems to me that the above does not work. But this, it would seem, contradicts the "not consuming".
Can someone define what is meant by non-consumer and show me a regular expression that will give the desired results?
zzxyz source share