For Programmers: Free Programming Magazines  


Home > Archive > PERL Miscellaneous > June 2005 > Re: newbie regexp question: how to extract just the filename from









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Re: newbie regexp question: how to extract just the filename from
Wolfman's Brother

2005-06-10, 3:58 pm

axel@white-eagle.invalid.uk wrote:

> James Calivar <amheiserbush@yahoo.com.au> wrote:
>
> my $instr = "C:/Program Files/Microsoft Office Tools/excel.exe";
> $instr =~ m#([^/]*)$#;
> print $1;
>
> yields
>
> excel.exe
>
> (and an empty string is the file/pathname ends in '/').
>
> However this will not work with a path/filename that has a drive letter but
> no directory separators (e.e. 'C:foo.bar').


To handle the ":", just add ":" to the pattern.

$instr =~ m#([^/:]*)$#;

But *this* doesnt handle the case of backslashes instead of forward
(windows allows both), so try..

$instr =~ m#([^/:\\]*)$#;

Chris

--
http://www.lowth.com/rope - Scripting linux's iptables/netfilter.
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2009 codecomments.com