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

Hi Monks,
i have a file will following entries
\windows\server\locales\en_US\help\whxdata\whftdata0.xml \windows\server\locales\en_US\help\whxdata\ \windows\server\locales\en_US\help\whxdata\whfwdata0.xml \windows\server\locales\en_US\help\whxdata\whfwdata1.xml \windows\server\locales\en_US\help\whxdata\whfwdata2.xml \windows\server\locales\en_US\help\whxdata2\ <\code> Here i have to find and list only the directory . In this case line 2& +6. i am using <code> if $line =~ /(.*)\\$/{ ....
It is not matching. Please correct the Reg exp if wrong.

Replies are listed 'Best First'.
Re: Reg Exp help
by toolic (Bishop) on Jun 04, 2008 at 18:36 UTC
    This works for me:
    use strict; use warnings; while (<DATA>) { if (/(.*)\\$/) {print "$1\n"} } __DATA__ \windows\server\locales\en_US\help\whxdata\whftdata0.xml \windows\server\locales\en_US\help\whxdata\ \windows\server\locales\en_US\help\whxdata\whfwdata0.xml \windows\server\locales\en_US\help\whxdata\whfwdata1.xml \windows\server\locales\en_US\help\whxdata\whfwdata2.xml \windows\server\locales\en_US\help\whxdata2\

    prints:

    \windows\server\locales\en_US\help\whxdata \windows\server\locales\en_US\help\whxdata2

    Also, please fix your first closing "code" tag: it should be "/code", not "\code".

Re: Reg Exp help
by sadfaceman (Novice) on Jun 04, 2008 at 18:51 UTC
    you could always use the built in module File::Basename, and then use fileparse.
    #!/usr/bin/env perl #use strict; #use warnings; use File::Basename; while (<DATA>) { my($name, $dir, $ext) = (fileparse($_,'\..*')); print $dir,"\n" if !$ext; } __DATA__ \windows\server\locales\en_US\help\whxdata\whftdata0.xml \windows\server\locales\en_US\help\whxdata\ \windows\server\locales\en_US\help\whxdata\whfwdata0.xml \windows\server\locales\en_US\help\whxdata\whfwdata1.xml \windows\server\locales\en_US\help\whxdata\whfwdata2.xml \windows\server\locales\en_US\help\whxdata2\
    Produces
    \windows\server\locales\en_US\help\whxdata\ \windows\server\locales\en_US\help\whxdata2\
Re: Reg Exp help
by hilitai (Monk) on Jun 04, 2008 at 19:00 UTC
    What result are you getting? Is it matching too much, or too little? Are you sure that the '\' is really the last character on the line, or are there non-printing characters (e.g., spaces or tabs) after it?
Re: Reg Exp help
by Anonymous Monk on Jun 04, 2008 at 18:43 UTC

    Try saying out loud what you want to match: I want everything from the beginning of the line to the last backslash,

    ^.*\\

    then possibly some more stuff

    .*

    and then the end of the line.

    $

    So, seems like it oughta be:

    if ( $line =~ m/^(.*)\\.*$/ ) { ... }

    As long as the directory-only lines always end with a backslash. Does that one work for you?

      Whoops. Read your question wrong. The above looks for all directory names.

Re: Reg Exp help
by poolpi (Hermit) on Jun 05, 2008 at 14:16 UTC

    See File::Spec

    #!/usr/bin/perl -w use strict; use File::Spec::Win32; use Data::Dumper; my %h_dirs; while (<DATA>){ chomp; my ($vol, $dirs, $file) = File::Spec::Win32->splitpath($_); $h_dirs{ $dirs }++ unless $file ; } print Dumper \%h_dirs; __DATA__ \windows\server\locales\en_US\help\whxdata\whftdata0.xml \windows\server\locales\en_US\help\whxdata\ \windows\server\locales\en_US\help\whxdata\whfwdata0.xml \windows\server\locales\en_US\help\whxdata\whfwdata1.xml \windows\server\locales\en_US\help\whxdata\whfwdata2.xml \windows\server\locales\en_US\help\whxdata2\
    Output: $VAR1 = { '\\windows\\server\\locales\\en_US\\help\\whxdata2\\' => 1, '\\windows\\server\\locales\\en_US\\help\\whxdata\\' => 1 };

    hth,
    PooLpi

    'Ebry haffa hoe hab im tik a bush'. Jamaican proverb