Regexp question...

Discussion in 'Perl Misc' started by adamomitcheney@kiwis.co.uk, Jun 13, 2005.

  1. Guest

    Hi All,

    Apologies in advance if this is a dumb question ("There are no stupid
    questions, but there are loads of inquisitive idiots")...

    I wanted a regular expression to seperate a filename from a full path
    (UNIX). I came up with the following:

    $target =~ m%(.+/)(.+)%;
    $targetpath = $1;
    $targetfile = $2;

    Actually, I expected that not to work; but it does, apparently
    perfectly. I thought it would fail because I thought the $1 portion of
    the regexp would process $target until it found the leftmost '/' and
    then stop, leaving everything else to the $2 part. Why am I wrong? Am I
    just feeding it a particularly restrictive set of cases?

    What regular expression could I use if I wanted to split a UNIX path
    into the bottom level (that is, closest to root) directory and the rest
    of the path?

    Thanks for your indulgence.

    Adam...
    , Jun 13, 2005
    #1
    1. Advertising

  2. * schrieb:
    >
    > I wanted a regular expression to seperate a filename from a full path
    > (UNIX). I came up with the following:
    >
    > $target =~ m%(.+/)(.+)%;
    > $targetpath = $1;
    > $targetfile = $2;
    >
    > Actually, I expected that not to work; but it does, apparently
    > perfectly. I thought it would fail because I thought the $1 portion of
    > the regexp would process $target until it found the leftmost '/' and
    > then stop, leaving everything else to the $2 part. Why am I wrong?


    In regular expressions ".+" is greedy. Hence the first part process
    $target until the *rightmost* slash is found.

    >
    > What regular expression could I use if I wanted to split a UNIX path
    > into the bottom level (that is, closest to root) directory and the rest
    > of the path?


    Avoid that greediness. Please read `perldoc perlre` for details.

    $target =~ m%(.+?/)(.+)%;

    regards,
    fabian
    Fabian Pilkowski, Jun 13, 2005
    #2
    1. Advertising

  3. wrote in news::

    > I wanted a regular expression to seperate a filename from a full path
    > (UNIX).
    > I came up with the following:
    >
    > $target =~ m%(.+/)(.+)%;
    > $targetpath = $1;
    > $targetfile = $2;


    You do not need the full regex machinery to do accomplish such a
    task. In addition, using File::Basename is more appropriate than
    rolling your own in this case. See

    <URL: http://groups-beta.google.com/group/comp.lang.perl.misc/msg/e8d8519b712c483b?dmode=source>

    <URL: http://groups-beta.google.com/groups?q=unur+basename&scoring=d>

    Sinan

    --
    A. Sinan Unur <>
    (reverse each component and remove .invalid for email address)

    comp.lang.perl.misc guidelines on the WWW:
    http://mail.augustmail.com/~tadmc/clpmisc/clpmisc_guidelines.html
    A. Sinan Unur, Jun 13, 2005
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Greg Hurrell
    Replies:
    4
    Views:
    157
    James Edward Gray II
    Feb 14, 2007
  2. Mikel Lindsaar
    Replies:
    0
    Views:
    481
    Mikel Lindsaar
    Mar 31, 2008
  3. Joao Silva
    Replies:
    16
    Views:
    358
    7stud --
    Aug 21, 2009
  4. Uldis  Bojars
    Replies:
    2
    Views:
    190
    Janwillem Borleffs
    Dec 17, 2006
  5. Matìj Cepl

    new RegExp().test() or just RegExp().test()

    Matìj Cepl, Nov 24, 2009, in forum: Javascript
    Replies:
    3
    Views:
    178
    Matěj Cepl
    Nov 24, 2009
Loading...

Share This Page