Perl Regular expression needed

Posted on 2011-10-07
Last Modified: 2012-06-27
I have a path variable. It always starts with R:\\ and is followed by one or more directories beneath. I need a regular expression that keeps the 1st folder and removes the drive ( R:\\ ) and anything that follows the first folder. So all that will be left is the first folder name. Have a couple examples below to make it clear what I'm wanting.

Example 1
$file = R:\\SomeDirectory123\\BlaBlaBla\\Hello123\\
$folder =~ EXPRESSIONHERE that modifies $file;

Value of $folder should be: SomeDirectory123

Example 2
$file = R:\\SomeOtherDirectory321\\
$folder =~ EXPRESSIONHERE that modifies $file;

Value of $folder should be: SomeOtherDirectory321
Question by:KGNickl
    LVL 9

    Assisted Solution

    my $file = 'R:\\SomeDirectory123\\BlaBlaBla\\Hello123\\';
    my ($folder) = ($file =~ /\\(.*?)\\/);
    print "F $folder\n";

    Open in new window

    LVL 2

    Accepted Solution

    The comment by parparov is probably workable, but I would feel slightly safer anchoring the regex to the left hand end to avoid picking up the wrong directory: (And carrying out using two lines to be easier self-documenting:

    $file =~ m/^R:\\(.*?)\\/;
    $folder = $1;


    LVL 9

    Expert Comment

    The solution by roundel35 is indeed safest and securest if you wish to restrict everything to drive R:; it will fail if the drive letter changes. The minimal matching ensures the left-side anchoring and will get the top directory on a local drive or a network share.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Highfive Gives IT Their Time Back

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    I have been reconstructing a PHP-based application that has grown into a full blown interface system over the last ten years by a developer that has now gone into business for himself building websites. I am not incredibly fond of writing PHP code o…
    Many time we need to work with multiple files all together. If its windows system then we can use some GUI based editor to accomplish our task. But what if you are on putty or have only CLI(Command Line Interface) as an option to  edit your files. I…
    Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
    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…

    737 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

    15 Experts available now in Live!

    Get 1:1 Help Now