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

hi

can anybody help me in regular expression

i have string like

C:\Documents and Settings\darshan\Desktop\logo1.gif

and i want to extract the file name ie logo1.gif to the string i tried many ways but it didn't work

thanking you

darshan.


Edited: ~Thu Oct 10 00:14:53 2002 (GMT) by footpad: Retitled (was: Regular expression) and added HTML formatting, per Consideration

Replies are listed 'Best First'.
Re: Separating file names from path names
by kabel (Chaplain) on Oct 09, 2002 at 14:47 UTC
    have a look at the package File::Basename.
    C:\Perl Testskripte\580>perl use strict; use File::Basename; my $file = "c:\\asdf\\asdf\\asdf\\image.jpg"; print "dirname is [", dirname ($file), "]\n"; print "filename is [", basename ($file), "]\n"; ^D dirname is [c:\asdf\asdf\asdf] filename is [image.jpg] C:\Perl Testskripte\580>
Re: Separating file names from path names
by MZSanford (Curate) on Oct 09, 2002 at 15:29 UTC
    In the event you cannot use any other modules (as seems to be common with questions of this sort), and you want the file name, with extention ... you could use :
    my $file = $full_name; $file =~ s!.*\\!!g;
    I think this is covered in The Perl Cookbook quite well.
    from the frivolous to the serious
Re: Separating file names from path names
by foxops (Monk) on Oct 09, 2002 at 15:12 UTC
    This scans for anything until you hit a period, finds word chars to the left and anything to the right. Quick and dirty.
    $PATH = 'C:\Documents and Settings\darshan\Desktop\logo1.gif'; $PATH =~ /.*?(\w*?[\.].*)/s; print $1;