http://qs321.pair.com?node_id=184706

zejames has asked for the wisdom of the Perl Monks concerning the following question:

Fellow monks,

Here is my problem : I want to substitute a word, 'foo', by another word 'bar' in a line, but only if that line does not contain 'toto' before matching 'foo'.

Of course, I can do this :
while (<DATA>) { s/foo/bar/ if ($_ !~ /toto.*?foo/); print; } __DATA__ toto 4dsf4qsd foo mama 432fz foo
Output:
toto 4dsf4qsd foo mamaf 432fz bar

But what I am looking for is an only regex, probably using look-behing assertion. I have played with (?<!pattern), without success. As far as I understand, the problem is that there can be anything between 'toto' and 'foo'.

Any hint ?

--
zejames