in reply to regexp find last word

Using lookahead, you avoid replacing things with themselves:
s/(?=fox(?!.*fox.*forest).*forest)/the /;
If the next thing you see is fox, and it is not followed by some string containing fox followed later by forest, and it is followed by forest, then stick in "the ".

Alternatively:

s/(?=fox(?:(?!fox).)*forest)/the /;
If the next thing you see is fox, and it is followed by a sequence of characters, none of which starts another fox, and then you see forest, stick in "the ".

Caution: Contents may have been coded under pressure.

Replies are listed 'Best First'.
Re^2: regexp find last word
by ikegami (Patriarch) on Mar 02, 2005 at 15:43 UTC

    or s/(fox(?:(?!fox).)*forest)/the $1/;

    Update: nm, capture makes it slower.