http://qs321.pair.com?node_id=701618

rovf has asked for the wisdom of the Perl Monks concerning the following question:

I wanted to substitute the extension of a filename by 'txt', that is to transform abc/xyz.something into abc/xyz.txt. This is easy, but maybe driven by the sudden thought that I'm becoming an old man as the years passed by without ever having tried a positive look behind regexp, I came up with the following silly solution:

use strict; use warnings; my $fn="abc/def.xyz"; $fn =~ s/($<=[.])[^.]*$/txt/; print "$fn\n";
That is, substitute the longest string at the end of the filename which does not contain a period, but is preceded with one. Interestingly, this did not work - no substitution was taking place.

In my case this is overkill, because I happen to know in my filename that there *is* a period, so I could have much easier written

$fn =~ s/[^.]*$/txt/;
(this would however replace the complete filename with txt if it doesn't contain a period).

Nevertheless I would like to know *why* my original solution has failed. Any suggestions?

-- 
Ronald Fischer <ynnor@mm.st>