However, in the present case the last "fox" is the same as the last "fox" before "forest".
Well, yes. But if you think it's worthwhile to ignore the question of the OP, and solely focus on the example given, why not give as answer:
$_ = "fox comes and the fox goes into forest";
That surely works for the example given. It doesn't scale to fit the OP's requirements, but your solution doesn't either.

Without really exact requirements it's unclear.
Then at least state what's unclear, and which things you have implied in your answer. While I agree that the OP doesn't state there could be more than one forest in the sentence, he does write: put the word "the" before the nearest (or last fox) relative to forest. To me, that doesn't imply at all he wants to change the last fox. His problem mentions foxes and a forest - and their relationship.

guessed that the real question posed by the original poster had to do with finding the last "fox", and that the fact that he described it as the last "fox" "relative to forest" was coincidental.
If you try to outsmart the user, please, please do say so. Your guess could very well be right, but second guessing the person asking, without stating so is IMO a very bad thing to do. Just imagine your guess was wrong (and that the OP does want to find the last fox before the forest - just as he says), and noone pointed this (like I did). The OP happily takes your answer, only to find out later than your answer doesn't work. He then concludes help received on Perlmonks isn't useful, and never returns to this site. I agree, many people asking questions here ask the wrong question (and for many, had they asked the right question, they had known the answer) - but if you suspect this, and want to answer what you think his/her real question is, say so. It makes answers much more useful (and you'll attract less downvotes for "giving the wrong answer").

It is often quite useful to see some errors and realize just what went wrong in addition to some really slick solution.
Too bad that someone asking the question often can't know the difference between right and wrong answers, and than wrong answers not always get corrected, but often also get upvoted (from being the first to answer, or to appear to have given a correct answer).

In reply to Re^4: regexp find last word by Anonymous Monk
in thread regexp find last word by Murcia

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.