in reply to regexp find last word

A generic version.
$_ = "fox comes and fox goes into forest"; s!(\b(\w+)\b.*?)\2!$1the $2!; print $_;

Replies are listed 'Best First'.
Re^2: regexp find last word
by Anonymous Monk on Mar 02, 2005 at 15:37 UTC
    That puts 'the' in front the first word that appears twice. Which doesn't need to be 'fox', nor does it has anything to do with the last fox before the forest. It would fail on:
    "fox goes into forest"