Regex to split a filepath

Posted on 2009-04-28
Last Modified: 2012-05-06
i'm looking for a regex to split the filename from a path, eg
$_ = '/path/to/my//file/space /my music - name.mp3';

how do i get the filename (my music - name.mp3) and the path  (/path/to/my//file/space /)
into a variable, i'm trying to do something like
$MP3= '/path/to/my//file/space /my music - name.mp3';
($var,$value) = split(/\/([^.]*)/ig,$MP3,2);    

i'm lost, any idea ?

Question by:MKrauss
    LVL 39

    Accepted Solution


    $_ = '/path/to/my//file/space /my music - name.mp3';
    my ($var,$value) = /^(.*\/)(.*)$/;

    Open in new window

    LVL 1

    Author Comment

    that almost works, i would expect $value to be 'my music - name.mp3' but
    the above sample gives me 'name.mp3'
    LVL 1

    Author Comment

    got it

    Featured Post

    Threat Intelligence Starter Resources

    Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

    Join & Write a Comment

    I've just discovered very important differences between Windows an Unix formats in Perl,at least 5.xx.. MOST IMPORTANT: Use Unix file format while saving Your script. otherwise it will have ^M s or smth likely weird in the EOL, Then DO NOT use m…
    On Microsoft Windows, if  when you click or type the name of a .pl file, you get an error "is not recognized as an internal or external command, operable program or batch file", then this means you do not have the .pl file extension associated with …
    Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
    In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

    746 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    16 Experts available now in Live!

    Get 1:1 Help Now