in reply to Re: match last element instead of first??
in thread match last element instead of first??

... and m/fish(.*?)$/ makes perl go to the end of the string, match as little as possible (from back to front), and then fish.

Update: After rereading the original message that was not asked ;-). But it may still be worth noting that anchoring is a good idea if there is no compelling reason against it.

Second update: ignore this node ;)

Replies are listed 'Best First'.
Re^3: match last element instead of first??
by Corion (Patriarch) on Oct 17, 2007 at 17:30 UTC

    No:

    perl -le "$_=shift; m/fish(.*?)$/ and print $1" "one fish two fish thr +ee fish"

    gives

    two fish three fish

    .*? will match as little as necessary, but it will not match as little as possible. The fish will still match at the leftmost position. Then, the .*? will match as little as necessary to make the match succeed, which still is the rest of the string.