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).


In reply to Matching part of a path by hweefarn

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.