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

Hello I'm trying to split a local path after the second '\' for example: C:\VENDOR\MICROSOFT\PLATFORMSDK\INCLUDE first half: C:\VENDOR second half: \MICROSOFT\PLATFORMSDK\INCLUDE this is what I have for code:
use strict; foreach $file (@NetIncludeDir) { ($network, $path) = $file =~ m<^( (?:\+ \w+){2} )(.*)>x; print "Network: $network, Path: $path\n"; }
Unfortunatley, No luck, any advice on what I've done wrong?

Replies are listed 'Best First'.
Re: regular expression help
by Abigail-II (Bishop) on Jun 11, 2002 at 13:03 UTC
    Well, one obvious problem is that you didn't escape the backslash, and ended up with \+ which escapes the plus sign.

    I wouldn't use a compilicated regex, but a simple split.

    { my @chunks = split /\\/ => $file, 3; my $network = join "\\" => @chunks [0, 1]; my $path = $chunks [2]; ... }

    Abigail

Re: regular expression help
by broquaint (Abbot) on Jun 11, 2002 at 13:05 UTC
    Hmmm that regex looks familiar ;-)
    The problem is that you're still treating it like a network path instead of a file path. Here's a slight mod that should make it do what you want
    my @NetIncludeDir = qw(C:\VENDOR\MICROSOFT\PLATFORMSDK\INCLUDE); foreach my $file (@NetIncludeDir) { my($network, $path) = $file =~ m<^([\w:]+ (?:\\+ \w+) )(.*)>x; print "File: $file\nNetwork: $network, Path: $path\n"; } __output__ File: C:\VENDOR\MICROSOFT\PLATFORMSDK\INCLUDE Network: C:\VENDOR, Path: \MICROSOFT\PLATFORMSDK\INCLUDE

    HTH

    _________
    broquaint

      It should, its some of your code :) I thought I code just revise some of your code from our previous discussion, but I think I am making things to complicated. Thanks Again!
Re: regular expression help
by DamnDirtyApe (Curate) on Jun 11, 2002 at 13:05 UTC
    my $test_str = 'C:\VENDOR\MICROSOFT\PLATFORMSDK\INCLUDE' ; my ( $first, $second ) = $test_str =~ m/^([^\\]+\\[^\\]+)(\\.*)$/ ; print "first: $first\nsecond: $second\n" ; # first: C:\VENDOR # second: \MICROSOFT\PLATFORMSDK\INCLUDE

    _______________
    D a m n D i r t y A p e
    Home Node | Email
      That would fail if the string started with a backslash, or if there's nothing between the first and the second backslash. Turning the +'s to *'s will fix that glitch.

      Abigail