in reply to Simple Regex needed

There are two simple ways of answering this question. One is to give you what you asked for: a regex to get the filename component from a UNIX-style path. That regex is m|(.[^/]+)$/ (Read: a bunch of non-slash characters (the maximum number possible, more or less), then the end of the string (with various caveats on exactly what the end of string means).

The other is to inform you of the right way to do it, which is to use File::Basename, which will do the right thing regardless of platform, and also makes the intent of your code more clear.


Warning: Unless otherwise stated, code is untested. Do not use without understanding. Code is posted in the hopes it is useful, but without warranty. All copyrights are relinquished into the public domain unless otherwise stated. I am not an angel. I am capable of error, and err on a fairly regular basis. If I made a mistake, please let me know (such as by replying to this node).

Replies are listed 'Best First'.
Re: Re: Simple Regex needed
by agentv (Friar) on May 08, 2003 at 20:52 UTC
    theorbtwo wrote: >  That regex is m|(.[^/]+)$/

    Thanks for that answer, which I think is correct in several respects (including the way it provides a direct answer to the question as originally posed alongside the statement of fact that there might be an easier way to do the job).

    The one thing I would point out is that maybe you meant to say:

     m|(.[^/]+)$| -or-

     /(.[^/]+)$/

    ...All the world looks like -well- all the world, when your hammer is Perl.
    ---v