Amitabha
Amitabha

Reputation: 1713

Perl Regex Extract first two section of windows path

I want to write a method to extract first two sections of windows path in Perl.

For example, 'D:\git_root_tfs\WorkStation\Projects\InterACT\Tools\server-rule-checker'

Extract as: 'D:\git_root_tfs\WorkStation'

sub Split_Location_as_VMPath {
    my $location = shift;
    # ^([d-z]:\\.+?\\.+?)\\
    # ^(?:\\.*\\.*)\\
    if($location ~~ m/^(?:\\.*\\.*)\\/){ # the path drive start from D to E;
        # print "VMPath=$1\n";          
        # push @$vmPathList, $1;
        return Convert_to_Lowercase($1);
    }
    return "Invalid Path $location";
}

How to write the regex?

Test case:

{
    my $item = Split_Location_as_VMPath('D:\VM\ia7-BGCDev8.1\test.vhd');
    my $expected = Convert_to_Lowercase('D:\VM\ia7-BGCDev8.1');
    ok( $item eq $expected, "Test Split_Location_as_VMPath=$item");

    $item = Split_Location_as_VMPath('E:\Hyper-V-2\ia-int-7.1Beta\test.vhd');
    $expected = Convert_to_Lowercase('E:\Hyper-V-2\ia-int-7.1Beta');
    ok( $item eq $expected, "Test Split_Location_as_VMPath=$item");

    $item = Split_Location_as_VMPath('D:\VM\ia7-int-7.1\test.vhd');
    $expected = Convert_to_Lowercase('D:\VM\ia7-int-7.1');
    ok( $item eq $expected, "Test Split_Location_as_VMPath=$item");

    $item = Split_Location_as_VMPath('D:\VM\ia7-int-8.1B153\test.vhd');
    $expected = Convert_to_Lowercase('D:\VM\ia7-int-8.1B153');
    ok( $item eq $expected, "Test Split_Location_as_VMPath=$item");

    $item = Split_Location_as_VMPath('D:\Hyper-v\IA5-SDE-WIN2K3(Feng Tong)\test.vhd');
    $expected = Convert_to_Lowercase('D:\Hyper-v\IA5-SDE-WIN2K3(Feng Tong)');
    ok( $item eq $expected, "Test Split_Location_as_VMPath=$item");

    $item = Split_Location_as_VMPath('D:\git_root_tfs\WorkStation\Projects\InterACT\Tools\server-rule-checker');
    $expected = Convert_to_Lowercase('D:\git_root_tfs\WorkStation');
    ok( $item eq $expected, "Test Split_Location_as_VMPath=$item");
}

Upvotes: 0

Views: 74

Answers (3)

BOC
BOC

Reputation: 1139

Using regex in this context is an interesting homework for students. Outside school, you should use the standard modules dedicated for this task:

use File::Spec;
sub Split_Location_as_VMPath {
    my $location = shift;
    my ($volume, $directories, $file) = File::Spec->splitpath($location);
    my @dirs = File::Spec->splitdir($directories);
    return "Invalid Path $location" unless @dirs > 2;
    return lc File::Spec->catpath($volume, File::Spec->catdir(@dirs[0..2]));    
}

Upvotes: 0

Miller
Miller

Reputation: 35198

Don't use a regex for file processing.

Instead use a module like File::Spec or Path::Tiny.

use strict;
use warnings;

use File::Spec;

while (<DATA>) {
    my ($vol, $dir, $file) = File::Spec->splitpath($_);
    my @dirs = File::Spec->splitdir($dir);
    @dirs = @dirs[0..2] if @dirs > 3;
    $dir = File::Spec->catdir(@dirs);
    my $path = File::Spec->catpath($vol, $dir);

    print "$path\n";
}

__DATA__
D:\VM\ia7-BGCDev8.1\test.vhd
E:\Hyper-V-2\ia-int-7.1Beta\test.vhd
D:\VM\ia7-int-7.1\test.vhd
D:\VM\ia7-int-8.1B153\test.vhd
D:\Hyper-v\IA5-SDE-WIN2K3(Feng Tong)\test.vhd
D:\git_root_tfs\WorkStation\Projects\InterACT\Tools\server-rule-checker

Outputs:

D:\VM\ia7-BGCDev8.1
E:\Hyper-V-2\ia-int-7.1Beta
D:\VM\ia7-int-7.1
D:\VM\ia7-int-8.1B153
D:\Hyper-v\IA5-SDE-WIN2K3(Feng Tong)
D:\git_root_tfs\WorkStation

Upvotes: 1

Amitabha
Amitabha

Reputation: 1713

Correct regex is ^([d-z]:\.+?\.+?)\.

sub Split_Location_as_VMPath {
    my $location = shift;
    # ^([d-z]:\\.+?\\.+?)\\
    # ^(?:\\.*\\.*)\\
    if($location ~~ m/^([D-Z]:\\.+?\\.+?)\\/){ # the path drive start from D to E;
        # print "VMPath=$1\n";          
        # push @$vmPathList, $1;
        return Convert_to_Lowercase($1);
    }
    return "Invalid Path $location";
}

Upvotes: 0

Related Questions