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:
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.use strict; use warnings; my $fn="abc/def.xyz"; $fn =~ s/($<=[.])[^.]*$/txt/; print "$fn\n";
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
(this would however replace the complete filename with txt if it doesn't contain a period).$fn =~ s/[^.]*$/txt/;
Nevertheless I would like to know *why* my original solution has failed. Any suggestions?
--
Ronald Fischer <ynnor@mm.st>
Ronald Fischer <ynnor@mm.st>
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: positive look behind regexp mystery
by moritz (Cardinal) on Aug 01, 2008 at 08:41 UTC | |
by rovf (Priest) on Aug 01, 2008 at 09:55 UTC | |
Re: positive look behind regexp mystery (\K assertion)
by lodin (Hermit) on Aug 14, 2008 at 12:54 UTC | |
by rovf (Priest) on Aug 14, 2008 at 13:28 UTC | |
by lidden (Curate) on Aug 14, 2008 at 14:31 UTC | |
by massa (Hermit) on Aug 14, 2008 at 14:42 UTC | |
by rovf (Priest) on Aug 19, 2008 at 08:27 UTC | |
Re: positive look behind regexp mystery
by BrowserUk (Patriarch) on Aug 01, 2008 at 08:48 UTC | |
Re: positive look behind regexp mystery
by Anonymous Monk on Aug 01, 2008 at 08:32 UTC |
Back to
Seekers of Perl Wisdom