tags:

views:

80

answers:

3

Hello, I need a regular expression to parse a text The directory is /home/foo/bar/hello.txt. I would like to get the hello.txt and get rid of the rest of the directory, so i would like to delete home/foo/bar/ and only get the hello.txt

+3  A: 

If you need to do things like that it means that you're using a wrong approach to construct your URIs. You don't need to parse things you construct, you need to have a schema for your URIs.

Update: For files in a filesystem, use File::Basename

codeholic
Well the way i wrote it, I never visit actually visit,so it would be difficult to get the request URI
Zerobu
Edited my problem according to my needs
Zerobu
A: 

update: should work for paths as well :-)

my ($filename) = $dir =~ m!^.*/(.*)!;

or

my $filename = (split '/', $dir)[-1];
eugene y
A: 

I suggest the following equivalent regexes :

 my ($extract1) = m { ( [^/]* ) $ }x ;      

and with comments added

 my ($extract2) = m {  
     (           # start capturing
       [^/] *    # anything other than / repeated
     )           # start capturing      
     $           # anchor to end of string  
  }x  ;

if you add the x at the end then you can add white space and comments to help explain the regex.

justintime