in reply to Combining regexes
Yet another one-liner. This is a bit tricky, and I can't say I'm particularly proud of it. It assumes $startdir always has a trailing \ (backslash) delimiter. Needs Perl version 5.14+ for the s/// substitution /r modifier.
But again, I must say I can't see anything really wrong with the basic approach of the OPed code.c:\@Work\Perl\monks>perl -wMstrict -le "use 5.014; ;; my $curdir = 'Y:\Music\Schubert\Lieder\Terfel'; my $startdir = 'Y:\mUsIc\\'; ;; my $plsname = $curdir =~ s{ \A (?i) \Q$startdir\E | \\ }{ $-[0] ? '_' : '' }xmsre +g . '.pls'; print qq{'$plsname'}; " 'Schubert_Lieder_Terfel.pls'
Update: Even more perverse, but requires no /r modifier, so it can run under pre-5.14, and no /e modifier:
The Devil (but not Terfel) finds work for idle hands.c:\@Work\Perl\monks>perl -wMstrict -le "print qq{perl version $]}; ;; my $curdir = 'Y:\Music\Schubert\Lieder\Terfel'; my $startdir = 'Y:\mUsIc\\'; ;; my %replace = ('1', '', '-1', '_', '0', '.pls'); (my $plsname = $curdir) =~ s{ \A (?i) \Q$startdir\E | \\ | \z } {$replace{ $-[0] == 0 || $-[0] <=> $+[0] }}xmsg; print qq{'$plsname'}; " perl version 5.008009 'Schubert_Lieder_Terfel.pls'
Update 2: Ok, slightly simpler, but needs captures:
Good luck maintaining this.c:\@Work\Perl\monks>perl -wMstrict -le "print qq{perl version $]}; ;; my $curdir = 'Y:\Music\Schubert\Lieder\Terfel'; my $startdir = 'Y:\mUsIc\\'; ;; my %replace = ('1', '', '2', '_', '3', '.pls'); (my $plsname = $curdir) =~ s{ (\A (?i) \Q$startdir\E) | (\\) | (\z) } {$replace{ $#- }}xmsg; print qq{'$plsname'}; " perl version 5.008009 'Schubert_Lieder_Terfel.pls'
Give a man a fish: <%-{-{-{-<
|
|---|