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

Hi,

Looking for Perl code using regular expressions to validate windows path.

For example:

my $dir = "c:\"; if ($dir =~ /REGULAR EXPRESSIONS/) { print ("Valid path: $dir"); } else { print ("Invalid path: $dir"); }

Thanks.

Replies are listed 'Best First'.
Re: How to validate windows path
by nemesdani (Friar) on Mar 13, 2012 at 10:17 UTC
    No regexp needed, simply check if $dir exists with the -e test.
    if -e $dir print "valid path";
    etc...

      $ perl -le " print -e qq'C:/\0blah'
      1

      $ perl -MPath::Class -le " print file(qq'C:/\0blah')->resolve "
      C:\

      No, my program get path from the user as parameter and need to check if the path is valid - if yes then create the folder.

        Define 'valid'. One approach would be to 'try it and see'. Attempt the create, if that succeeds, check to verify the directory exists1. In this case, 'valid' means being able to be created.

        If you define what you mean by 'valid', it may help with the responses.

        Footnotes:

        1. 1 - Winding back if it doesn't exist might be fun...

        Updates:

        1. +10 minutes - Update formatting and footnotes, and added a more generic response.

        --MidLifeXis