Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Matching part of a path

by hweefarn (Acolyte)
on Dec 16, 2003 at 06:38 UTC ( [id://314993] : perlquestion . print w/replies, xml ) Need Help??

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

hi everyone :)

i am a newbie to perl. i just learn some basic of string filtering from perlintro. now i want to filter a string (which is a full pathname) to get a folder name. but i still cant get what i want. can anyone pls help me? below is my code:
$a = "/home/a/ff.pl"; print "full path name is ", $a, "\n"; if ( $a =~ /^(\/home\/)(\S+).*$/ ) { print "\$1 = ", $1, "\n"; # output is /home/ print "\$2 = ", $2, "\n"; # output is a/ff.pl }
how can i get the output "a"? i try this
if ($a =~ /^(\/home\/)(\W)(\S+).*$/)
but cannot.

for your information, the pattern of the beginning part of the pathname will always be same, only the file name at the end will be different. sample input are: /home/a/ff.pl, /home/a/bb.pl, /home/b/cc.pl. that's all. thank you very much :)

regards,
hweefarn

update (broquaint): tidied up formatting + title change (was filter a string).

Replies are listed 'Best First'.
Re: Matching part of a path
by Aristotle (Chancellor) on Dec 16, 2003 at 07:11 UTC
    Most of the time you should use File::Spec to manipulate paths. It has been bundled with Perl for forever, so there's no reason not to.
    use File::Spec::Functions qw(canonpath splitdir); my (@path) = splitdir canonpath $path; # @path now contains ('', 'home', 'a', 'ff.pl') print $path[2], "\n";
    But just as a point of interest, you have two alternatives: a regex-only solution would be simply
    my ($userhome) = ($path =~ m!^/home/([^/]+)!);
    This will match a string that starts with /home/, followed by at least one character that is not a slash, and will capture all these non-slash characters. The other way would be to split on slashes:
    my @path = split m!/!, $path; # @path is now ('', 'home', 'a', 'ff.pl') print $path[2], "\n";

    Note how I used $path instead of your $a. This is for two reasons - first of all, $a and $b carry a special meaning for the sort function in Perl, so you shouldn't use them elsewhere. And secondly, $a is not very descriptive. Variables and functions should always have descriptive names, lest you find yourself boggling at your own code after putting it aside for two weeks.

    Also note how I used m!! instead of // to delimit the regular expressions. If you explicitly mention the m, Perl allows you to pick something other than forward slashes as delimiters, so you won't need to backwhack forward slashes inside the pattern. This is very handy to avoid "leaning toothpick syndrome". This and more is described in perldoc perlop.

    Makeshifts last the longest.

      Note how I used $path instead of your $a. This is for two reasons - first of all, $a and $b carry a special meaning for the sort function in Perl, so you shouldn't use them elsewhere. And secondly, $a is not very descriptive. Variables and functions should always have descriptive names, lest you find yourself boggling at your own code after putting it aside for two weeks.

      Is this really true? You could use in sort $me and $my as well (any for that matter)? It should complain though if you are using scalars you've set up before (like doing double my).
        Quoting perldoc -f sort:
        If the subroutine's prototype is ($$), the elements to be compared are passed by reference in @_, as for a normal subroutine. This is slower than unprototyped subroutines, where the elements to be compared are passed into the subroutine as the package global variables $a and $b (see example below). Note that in the latter case, it is usually counter-productive to declare $a and $b as lexicals.
        Perl only complains when you try to doubly declare a lexical variable name in the same scope. The following won't produce a complaint:
        use strict; use warnings; my $foo; { my $foo; }

        Makeshifts last the longest.

Re: Matching part of a path
by edoc (Chaplain) on Dec 16, 2003 at 07:00 UTC
    $a =~ /^(\/home\/)(\S+).*$/

    (\S+) will capture one or more non-space characters, so you get your 'a/ff.pl'

    $a =~ /^(\/home\/)(\W)(\S+).*$/

    (\W) will capture a single non word character, so it gets nothing.

    $a =~ /^\/home\/(\w)/  # note the lowercase 'w'

    (\w) will capture a single word character. (it will go into $1 as you don't require any other captures).

    cheers,

    J

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://314993]
Approved by davido
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (3)
As of 2024-02-24 13:52 GMT
Voting Booth?
My favourite way to spend a leap day ...











Results (22 votes). Check out past polls.