Repost, with a working regex (and no -w complaints).
>At 15:16 -0400 7/9/2001, Morbus Iff wrote:
>> > $f =~ m/:(.*)$/ ? print "$1\n" : print "$f\n";
>>
>>This will fail if you're more than one directory deep, ie, it'll work fine
>>on "HD:filename.xml", but will fail on "HD:Directory:filename.xml". In the
>>first case, it will properly return "filename.xml", in the second case, it
>>will return "Directory:filename.xml".
>>
>>This small modification will fix that:
>>
>> $f =~ m/:([^:]$.*)$/ ? print "$1\n" : print "$f\n";
>>
>>
>Actually, I don't understand the $ after the character class in that one.
>Neither does the -w flag, although the right answer is generated without
>the -w--or with it if your eye ignores the warning
>Use of uninitialized value in concatenation (.) at t.pl line 9.
>The path really only has one right end.
See below for a short, working I think regex.
>
>
>TIMTOWTDI
>
>One could if desired eschew both the regular expression and the ?: operator:
>
% cat t.pl
$a="Disk:Folder:Folder2:file";
$b="file";
$c="Disk:Folder:Folder2:";
print substr($a, rindex($a, ':') + 1), "\n";
print substr($b, rindex($b, ':') + 1), "\n";
print substr($c, rindex($c, ':') + 1), "\n";
$a =~ m/:([^:]*)$/ ? print "$1\n" : print "$a\n";
$b =~ m/:([^:]*)$/ ? print "$1\n" : print "$b\n";
$c =~ m/:([^:]*)$/ ? print "$1\n" : print "$c\n";
% perl -w t.pl
file
file
file
file
[localhost:/tmp] john%
Prior posts' mutterings about "better" stand as muttered. Plus...can we do
something in the regex to cut down the amount of backtracking? (As above,
Perl will start at the left and advance rightward to the first : then find
a string of non-colons, note that the end of the string isn't there, then
give up on that colon and walk along to the next one, and non-colons again,
noting that it isn't yet at end of string and so on until it encounters the
"right" colon.)
And I think there is:
m/(:?[^:]:)*([^:]*)$/
The first (...)* should* greedily match all the :non-colon: sequences
(including the leading one which doesn't start with :) I've also tested a
couple of Macintosh relative paths ($d and $e) below.
* I wonder whether it really does: the regex will still work if it doesn't
match anything. Exercise for the few remaining interested readers. And is
it faster? (Another exercise.)
I'd likely use rindex and substr and not worry about it.
And we haven't decided what the right answer is for a directory path:
$c="Disk:Folder:Folder2:";
(my methods produce the empty string for that...which probably isn't the
right answer, although it IS easy to test for, and so it might be correct).
Let's see:
% cat tt.pl
$a="Disk:Folder:Folder2:file-a";
$b="file-b";
$c="Disk:Folder:Folder2:";
$d=":file-d";
$e="::folder:folder2:file-e";
print substr($a, rindex($a, ':') + 1), "\n";
print substr($b, rindex($b, ':') + 1), "\n";
print substr($c, rindex($c, ':') + 1), "\n";
print substr($d, rindex($d, ':') + 1), "\n";
print substr($e, rindex($e, ':') + 1), "\n";
print "Start regex:\n";
$a =~ m/(:?[^:]*:)*([^:]*)$/ ? print "$2\n" : print "$a\n";
$b =~ m/(:?[^:]*:)*([^:]*)$/ ? print "$2\n" : print "$b\n";
$c =~ m/(:?[^:]*:)*([^:]*)$/ ? print "$2\n" : print "$c\n";
$d =~ m/(:?[^:]*:)*([^:]*)$/ ? print "$2\n" : print "$d\n";
$e =~ m/(:?[^:]*:)*([^:]*)$/ ? print "$2\n" : print "$e\n";
% perl -w tt.pl
file-a
file-b
file-d
file-e
Start regex:
file-a
file-b
file-d
file-e
--John (the overflowing verbiage)
--
John Baxter [EMAIL PROTECTED] Port Ludlow, WA, USA