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 :
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
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 :
Output:while (<DATA>) { s/foo/bar/ if ($_ !~ /toto.*?foo/); print; } __DATA__ toto 4dsf4qsd foo mama 432fz foo
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
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: look-behind regex
by Abigail-II (Bishop) on Jul 24, 2002 at 09:50 UTC | |
by japhy (Canon) on Jul 24, 2002 at 12:55 UTC | |
by Anonymous Monk on Jul 25, 2002 at 02:20 UTC | |
Re: look-behind regex
by DamnDirtyApe (Curate) on Jul 24, 2002 at 05:46 UTC | |
Re: look-behind regex
by Courage (Parson) on Jul 24, 2002 at 05:55 UTC | |
Re: look-behind regex
by I0 (Priest) on Jul 24, 2002 at 23:49 UTC | |
by Anonymous Monk on Jul 25, 2002 at 02:21 UTC | |
by I0 (Priest) on Jul 25, 2002 at 14:51 UTC | |
by Anonymous Monk on Jul 25, 2002 at 15:18 UTC | |
by I0 (Priest) on Jul 25, 2002 at 22:35 UTC | |
Re: look-behind regex
by flocto (Pilgrim) on Jul 24, 2002 at 10:22 UTC |
Back to
Seekers of Perl Wisdom